⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 linux下的FFT 频谱分析
💻 C
字号:
/* * Initial main.c file generated by Glade. Edit as required. * Glade will not overwrite this file. */#ifdef HAVE_CONFIG_H#  include <config.h>#endif#include <gtk/gtk.h>#include <string.h>#include <sys/stat.h>#include <stdarg.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <signal.h>#include <fcntl.h>#include <sys/mman.h>#include "interface.h"#include "support.h"#include "fft-spectra.h"static struct shmid_ds shmid_struct;static void *shm_address=NULL;static int shm_id=-1;char *config_file=NULL;void usage(void){    printf(        "Usage: fft-spectra [OPTIONS] [file]\n\n"        "Options:\n"        "    -b,  --bps  <int>               bytes per sample (default is %d)\n"        "    -c,  --channels <int>           mono=1, stereo=2 (default is %d)\n"        "    -C,  --config <file>            read settings from the given file (default is %s)\n"        "    -g,  --spectrogram              run in the spectrogram display mode\n"        "    -P,  --do-not-play              when reading from file, do not echo the sound\n"        "    -r,  --refresh-rate <int>       refresh x times per second (default is %d Hz)\n"        "    -R,  --esd-rate <int>           sampling rate (default is %d Hz)\n"        "    -s,  --settings <string>        use settings for the given session (read README)\n"        "    -u,  --unsigned                 take the input data as unsigned\n"        "    -n,  --nsamples <int>           use this many samples for fourier analysis (default is %d)\n"        "    -w,  --waves                    run in the wave display mode\n"        "         --shm-id                   connect to the specified shared memory (tip: man ipcs)\n"        "         --dont-fork                assume that the sound reader is already up and running\n"        "Example:\n"        "    sox test.wav -s -w -c 1 test.raw; fft-spectra test.raw   .. to view spectra of a recorded file\n"        "    fft-spectra -s piano -n 32768                            .. to get higher frequency density up to the default sampling rate (slow)\n"        "    fft-spectra -s xylophone  -R 8192 -n 4096                .. to get higher frequency density up to the 4000Hz (prefered)\n"        "\n",         bps,channels,config_file,refresh_rate,esd_rate,ndata_in);    exit(1);}void exit_nicely(const char *format, ...){    va_list ap;    va_start(ap, format);    vfprintf(stderr,format,ap);    va_end(ap);    if ( msg_from_gui )        *msg_from_gui = MSG_EXIT_REQUEST;    if ( msg_from_reader )        *msg_from_reader = MSG_EXIT_REQUEST;    usleep(250000);    if ( esd_stream_rec!=-1 )    {        close(esd_stream_rec);        esd_stream_rec = -1;    }    if ( esd_stream_play!=-1 )    {        close(esd_stream_play);        esd_stream_play = -1;    }    if ( file_stream!=-1 )    {        close(file_stream);        file_stream = -1;    }    if ( shm_address )    {        shmdt(shm_address);        shm_address = NULL;    }    if ( shm_id!=-1 )    {        shmctl(shm_id, IPC_RMID, &shmid_struct);        shm_id = -1;    }    exit(1);}void ssigterm_handler(int signo, struct sigcontext sc){    exit_nicely("Sigterm caught. Exiting.\n");}int main (int argc, char *argv[]){    struct stat sbuf;    int i;    char *custom_settings=NULL;    char *play_file=NULL;    char *buf;        key_t shm_key;    int shm_size, do_fork=1;    buf = getenv("HOME");    i = strlen(buf)+14;    config_file = malloc(i);    snprintf(config_file,i,"%s/.fft-spectra",buf);    for (i=1; i<argc; i++)    {        if ( (!strcmp("-C",argv[i]) || !strcmp("--config",argv[i])) && ++i<argc )        {            free(config_file);            config_file = argv[i];            continue;        }        if ( (!strcmp("-s",argv[i]) || !strcmp("--settings",argv[i])) && ++i<argc )        {            custom_settings = argv[i];            continue;        }        if ( (!strcmp("-n",argv[i]) || !strcmp("--nsamples",argv[i])) && ++i<argc )        {            sscanf(argv[i],"%d",&ndata_in);            continue;        }        if ( (!strcmp("-r",argv[i]) || !strcmp("--refresh-rate",argv[i])) && ++i<argc )        {            sscanf(argv[i],"%d",&refresh_rate);            continue;        }        if ( (!strcmp("-u",argv[i]) || !strcmp("--unsigned",argv[i])) )        {            signed_data=0;            /* We can't play unsigned data with esound, so far... */            do_not_play=1;            continue;        }        if ( (!strcmp("-w",argv[i]) || !strcmp("--waves",argv[i])) )        {            mode=MODE_WAVES;            continue;        }        if ( (!strcmp("-g",argv[i]) || !strcmp("--spectrogram",argv[i])) )        {            mode=MODE_SPECTROGRAM;            continue;        }        if ( (!strcmp("-P",argv[i]) || !strcmp("--do-not-play",argv[i])) )        {            do_not_play=1;            continue;        }        if ( (!strcmp("-c",argv[i]) || !strcmp("--channels",argv[i])) && ++i<argc )        {            sscanf(argv[i],"%d", &channels);            continue;        }        if ( (!strcmp("-R",argv[i]) || !strcmp("--esd-rate",argv[i])) && ++i<argc )        {            sscanf(argv[i],"%d", &esd_rate);            continue;        }        if ( (!strcmp("-b",argv[i]) || !strcmp("--bps",argv[i])) && ++i<argc )        {            sscanf(argv[i],"%d", &bps);            continue;        }        if ( !strcmp("--shm-id",argv[i]) && ++i<argc )        {            sscanf(argv[i],"%d", &shm_id);            continue;        }        if ( !strcmp("--dont-fork",argv[i]) )        {            do_fork = 0;            continue;        }        if ( stat(argv[i],&sbuf)==0 )        {            play_file = argv[i];            continue;        }        usage();    }    if ( play_file && !do_not_play && esd_rate!=ESD_DEFAULT_RATE )        fprintf(stderr,"\n\n"                "**********************************************************\n"                "WARNING: Make sure that the rate of your sound file is %d!\n"                "**********************************************************\n\n", esd_rate);    signal(SIGINT, (void (*)(int))ssigterm_handler);    /* Initialize the shared memory segment for exchanging data between the        reader and GUI.  When a file will be played, the sound_data_buffer will         be obtained by means of mmap. Only status integeres writer_ok,reader_ok and         sound_data_end will be then shared via shm.    */    shm_key  = ftok(".", 'S');    if ( !play_file )        shm_size = ndata_in*bps*channels*10 + 3*sizeof(int);    else        shm_size = 3*sizeof(int);            if ( shm_id==-1 )    {        shm_id = shmget(shm_key, shm_size, IPC_CREAT|IPC_EXCL|0600);        if ( shm_id==-1 )         {             perror("shmget");             exit_nicely(NULL);         }    }    shm_address = shmat(shm_id, NULL, 0);    if ( shm_address == (char *)-1 ) { perror("shmat"); exit_nicely(NULL); }    msg_from_gui     = (int*)shm_address;    msg_from_reader  = ((int*)shm_address) + 1;    sound_data_end   = ((int*)shm_address) + 2;    *msg_from_gui    = MSG_OK;    *msg_from_reader = MSG_PAUSE_REQUEST;    fprintf(stderr,"shm_key=0x%x shm_id=%d\n",shm_key,shm_id);    if ( ! play_file )    {        sound_data_buffer = shm_address+3*sizeof(int);        sound_data_size = shm_size;    }    else    {        file_stream = open(play_file,O_RDONLY);        if (file_stream<0)             exit_nicely("Could not open file %s: %s.\n",play_file,strerror(errno));        sound_data_buffer = mmap((caddr_t)0,sbuf.st_size,PROT_READ, MAP_PRIVATE,file_stream,0);        sound_data_size = sbuf.st_size;    }    if ( do_fork && fork()==0 )    {        /* child process */        if (!play_file)            sound_data_client_record();        else            sound_data_client_play_file();        shmdt(shm_address);        shmctl(shm_id, IPC_RMID, &shmid_struct);        return 1;    }        /* Wait a while so that the reader may fill the buffers */    if ( !play_file )        fprintf(stderr,"Collecting data...\n");    while ( *msg_from_reader == MSG_PAUSE_REQUEST )        usleep(50000);    /* Weird things might happen during the sleep. */    if ( *msg_from_reader != MSG_OK )        exit_nicely(NULL);    /* Initialize the GTK stuff */#ifdef ENABLE_NLS    bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");    textdomain (GETTEXT_PACKAGE);#endif    gtk_set_locale ();    gtk_init (&argc, &argv);    add_pixmap_directory (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps");    init(custom_settings);    gtk_main();    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -