static void audio_close() { if (video_useAudio) { audio_free(); dsp_close(&dspz); } }
int audio_init(struct audio **audiop) { struct audio *audio; int err; audio = malloc(sizeof(*audio)); if (audio == NULL) { return errno; } memset(audio, 0, sizeof(*audio)); err = snd_pcm_open(&audio->pcm, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK); if (err < 0) { fprintf(stderr, "%s(): %d: %s\n", __func__, err, snd_strerror(err)); audio_free(audio); return -err; } err = snd_pcm_set_params(audio->pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 2, 44100, 1, /* allow resampling */ 1 * 1000000); /* latency (1s) */ if (err < 0) { fprintf(stderr, "%s(): snd_pcm_set_params(): %d (%s)\n", __func__, err, snd_strerror(err)); audio_free(audio); return -err; } err = snd_pcm_prepare(audio->pcm); if (err < 0) { fprintf(stderr, "%s(): snd_pcm_prepare(): %d (%s)\n", __func__, err, snd_strerror(err)); audio_free(audio); return -err; } *audiop = audio; return -err; }
int audio_main(int argc, char *argv[]) { t_mdxmini mini; char pcmpath_mem[1024]; char nlgpath_mem[1024]; char *nlgfile = NULL; char *pcmpath = NULL; char *wavfile = NULL; int opt; int rate = 44100; int nosound = 0; int nlg_log = 0; int len = -1; #ifdef _WIN32 freopen("CON", "wt", stdout); freopen("CON", "wt", stderr); #endif audio_sdl_init(); signal(SIGINT, audio_sig_handle); printf("MDXPLAY on SDL Version %s\n", MDXMINI_VERSION); if (argc < 2) { usage(); return 1; } while ((opt = getopt(argc, argv, "q:l:r:s:o:bpwhx")) != -1) { switch (opt) { case 'q': // pcm path pcmpath = optarg; break; case 'l': // length len = atoi(optarg); break; case 'b': // log without path(no arguments) nlg_log = NLG_SAMEPATH; nlgfile = NULL; nosound = 1; break; case 'r': // log with path nlg_log = NLG_NORMAL; nlgfile = optarg; break; case 'p': // no sound(no arguments) nosound = 1; break; case 's': // rate rate = atoi(optarg); break; case 'o': // output to wav file wavfile = optarg; break; case 'w': // verbose mode(no arguments) g_verbose = 1; break; case 'x': // view note mode(no arguments) g_viewnote = 1; break; case 'h': default: usage(); return 1; } } if (rate < 8000) rate = 8000; if (audio_init(rate)) { printf("Failed to initialize audio\n"); return 0; } pcm.on = 1; mdx_set_rate(rate); if (!pcmpath) { pcmpath_mem[0] = 0; char *home = getenv("HOME"); if (home) { pcmpath = pcmpath_mem; strcpy(pcmpath, home); strcat(pcmpath,_PATH_SEP); strcat(pcmpath, ".mdxplay"); strcat(pcmpath,_PATH_SEP); } } // play files for(;optind < argc; optind++) { char *playfile = argv[optind]; // make NLG log if (nlg_log) { // NLG filename is not given if (!nlgfile) { nlgpath_mem[0] = 0; nlgfile = nlgpath_mem; strcpy(nlgfile, playfile); char *p = strrchr(nlgfile, '.'); if (p) strcpy(p,".NLG"); else strcat(nlgfile,".NLG"); } printf("CreateNLG:%s\n",nlgfile); nlgctx = CreateNLG(nlgfile); } // open mdx if (mdx_open(&mini, playfile, pcmpath)) { printf("File open error: %s\n", playfile); CloseNLG(nlgctx); nlgctx = NULL; return 0; } if (nosound || wavfile) audio_loop_file(&mini, wavfile, rate, len); else audio_loop(&mini, rate, len); CloseNLG(nlgctx); nlgfile = NULL; nlgctx = NULL; // close mdx mdx_close(&mini); } audio_free(); return 0; }
int main(int argc, char** argv) { auint j; auint t; auint cdi = 0U; auint auc = 0U; uint16* lpt; uint16* rpt; uint16 edt; /* Event data (just one) */ auint mid; /* Mouse device id */ SDL_Event event; /* The event got from the queue */ rrpge_object_t* emu = NULL; /* Init message */ printf("\n"); printf("%s", main_appname); printf("\n\n"); printf("%s", main_appauth); printf("%s", main_copyrig); printf("\n"); /* Check arguments: need an application */ if (argc <= 1){ printf("Error: need an application to run as parameter!\n"); exit(1); }else{ printf("Opening %s...\n", argv[1]); main_app = fopen(argv[1], "rb"); if (main_app == NULL){ perror("Failed to open file"); exit(1); } } /* Initialize emulator library */ rrpge_init_lib(&main_malloc, &free); /* Attempt to initialize the emulator. Note that the app. binary load ** callback is blocking, so no need to implement any waiting here using ** rrpge_init_run(). */ emu = rrpge_new_emu(&main_cbpack); if (emu == NULL){ printf("Failed to allocate emulator state\n"); goto loadfault; } t = rrpge_init_run(emu, RRPGE_INI_RESET); if (t != RRPGE_ERR_OK){ printf("Failed to initialize emulator\n"); main_printrerr(t); goto loadfault; } mid = rrpge_dev_add(emu, RRPGE_DEV_POINT); /* Add mouse (pointing device) */ /* Initialize renderer */ render_reset(emu); /* Initialize SDL */ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)!=0) return -1; /* Try to set up the screen */ if (screen_set()!=0) return -1; SDL_WM_SetCaption(main_appname, main_appicon); /* Set up audio */ if (audio_set(2048U) != 0U) return -1; /* OK, let's go! */ printf("Entering emulation\n"); /* The main loop: It is more straightforward to exit from it with a suitable ** break at the proper place */ while(1){ /* Wait for some event... */ if (SDL_WaitEvent(&event)==0) return -1; /*Error?*/ /* An event is here: do something about it! */ if (event.type==SDL_USEREVENT){ if (event.user.code==AUDIO_EVENT){ t = 0U; /* No halt */ while (audio_needservice()){ /* Waiting for audio data from emulator */ do{ j = rrpge_run(emu, RRPGE_RUN_FREE); t = rrpge_gethaltcause(emu); if (t & RRPGE_HLT_AUDIO){ cdi++; } if (cdi >= 10){ cdi = 0U; printf("Audio events: %08d\n", auc); printf("Cycles: %08d\n", j); main_printstats(emu); main_printhalt(t); } /* Need proper exit point... */ }while ((t & (RRPGE_HLT_AUDIO | RRPGE_HLT_EXIT | RRPGE_HLT_STACK | RRPGE_HLT_INVKCALL | RRPGE_HLT_INVOP | RRPGE_HLT_FAULT | RRPGE_HLT_DETACHED | RRPGE_HLT_WAIT)) == 0U); if ((t & RRPGE_HLT_AUDIO) != 0U){ /* Audio data produced by emulator */ audio_getbufptrs(&lpt, &rpt); rrpge_getaudio(emu, lpt, rpt); auc++; } if ((t & (RRPGE_HLT_EXIT | RRPGE_HLT_STACK | RRPGE_HLT_INVKCALL | RRPGE_HLT_INVOP | RRPGE_HLT_FAULT | RRPGE_HLT_DETACHED | RRPGE_HLT_WAIT)) != 0U){ /* Errors & Exit */ break; } } /* End of audio wait loop */ if ((t & RRPGE_HLT_EXIT) != 0U){ break; } /* Exit program */ if ((t & (RRPGE_HLT_STACK | RRPGE_HLT_INVKCALL | RRPGE_HLT_INVOP | RRPGE_HLT_FAULT | RRPGE_HLT_DETACHED | RRPGE_HLT_WAIT)) != 0U){ main_errexit(t, emu); break; /* Also exit, with error */ } } }else if (event.type==SDL_KEYDOWN){ /* Some key was pressed. Which? */ if (event.key.keysym.sym==SDLK_ESCAPE) break; /* Exit program */ }else if (event.type==SDL_KEYUP){ }else if (event.type==SDL_QUIT){ /* Exit program */ break; }else if (event.type==SDL_MOUSEBUTTONDOWN){ edt = (uint16)(event.button.x); rrpge_dev_push(emu, mid, 2U, 1U, &edt); edt = (uint16)(event.button.y); rrpge_dev_push(emu, mid, 3U, 1U, &edt); if (event.button.button == SDL_BUTTON_LEFT){ edt = 1U; rrpge_dev_push(emu, mid, 0U, 1U, &edt); } else if (event.button.button == SDL_BUTTON_RIGHT){ edt = 2U; rrpge_dev_push(emu, mid, 0U, 1U, &edt); } else if (event.button.button == SDL_BUTTON_MIDDLE){ edt = 3U; rrpge_dev_push(emu, mid, 0U, 1U, &edt); } else {} }else if (event.type==SDL_MOUSEBUTTONUP){ edt = (uint16)(event.button.x); rrpge_dev_push(emu, mid, 2U, 1U, &edt); edt = (uint16)(event.button.y); rrpge_dev_push(emu, mid, 3U, 1U, &edt); if (event.button.button == SDL_BUTTON_LEFT){ edt = 1U; rrpge_dev_push(emu, mid, 1U, 1U, &edt); } else if (event.button.button == SDL_BUTTON_RIGHT){ edt = 2U; rrpge_dev_push(emu, mid, 1U, 1U, &edt); } else if (event.button.button == SDL_BUTTON_MIDDLE){ edt = 3U; rrpge_dev_push(emu, mid, 1U, 1U, &edt); } else {} }else if (event.type==SDL_MOUSEMOTION){ edt = (uint16)(event.motion.x); rrpge_dev_push(emu, mid, 2U, 1U, &edt); edt = (uint16)(event.motion.y); rrpge_dev_push(emu, mid, 3U, 1U, &edt); } } printf("Trying to exit\n"); rrpge_delete(emu); audio_free(); screen_free(); SDL_Quit(); fclose(main_app); exit(0); loadfault: if (emu != NULL) free(emu); fclose(main_app); exit(1); }
int main(int argc, char* argv[]) { SDL_Surface *screen; SDL_Event event; const SDL_VideoInfo *info; int keypress = 0; Audio *audio_input = NULL; printf("Spectrogram\n"); // Initialize microphone_init(); audio_input = NULL; size_t read_samples; if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1; info = SDL_GetVideoInfo(); if (info == NULL) { printf("Unable to get video info: %s\n", SDL_GetError()); return 1; } WIDTH = info->current_w; HEIGHT = info->current_h; #ifdef FORCE_WIDTH WIDTH = (FORCE_WIDTH); #endif #ifdef FORCE_HEIGHT HEIGHT = (FORCE_HEIGHT); #endif printf("Milliseconds per slice: %i\n", MS_IN_TIME_SLICE); printf("Maximum frequency: %.0f\n", FREQ_MULT*(HEIGHT-1)); printf("Rate of minimum frequency: %f\n", frequency( (((double)1) * FREQ_MULT) ) ); printf("Rate of maximum frequency: %f\n", frequency( (((double)(HEIGHT-1)) * FREQ_MULT) ) ); printf("Samples per slice: %ld\n", SAMPLES_IN_TIME_SLICE ); printf("Revolutions per slice: %.2f .. %.2f\n", (SAMPLES_IN_TIME_SLICE*frequency( (((double)1) * FREQ_MULT) ) ), (SAMPLES_IN_TIME_SLICE*frequency( (((double)(HEIGHT-1)) * FREQ_MULT) ) ) ); printf("Resolution: %ix%i\n", WIDTH, HEIGHT); int flags = 0; #ifndef NO_FULLSCREEN flags |= SDL_FULLSCREEN; #endif #ifndef NO_HWSUFACE flags |= SDL_HWSURFACE; #endif if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, flags))) { SDL_Quit(); return 1; } int xx = 0; fourier = audio_make_buffer( (HEIGHT/Y_STRETCH), DOUBLE_REAL ); while(!keypress) { audio_free( audio_input ); audio_input = audio_make_buffer( SAMPLES_IN_TIME_SLICE, MIC_FORMAT ); // Read in a slice of audio read_samples = microphone_read( SAMPLES_IN_TIME_SLICE, audio_input ); audio_convert( audio_input, DOUBLE_REAL ); if (windowing == 1) { #ifdef BARTLETT_WINDOW bartlett_window( audio_input ); #endif #ifdef BLACKMAN_HARRIS_WINDOW blackman_harris_window( audio_input ); #endif #ifdef HANN_WINDOW hann_window( audio_input ); #endif } // if (capturing) // DrawScreen( screen, captured, xx ); // else DrawScreen( screen, audio_input, xx ); xx += X_STRETCH; if (xx+X_STRETCH >= WIDTH) { xx = 0; } while(SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: keypress = 1; break; case SDL_KEYDOWN: if ( event.key.keysym.sym == SDLK_SPACE ) { capturing = !capturing; // If we just switched to capturing, reinitialize the capturing slice if (capturing) { audio_free( captured ); captured = audio_make_buffer( (HEIGHT/Y_STRETCH), DOUBLE_REAL ); // Zero the audio by amplifying it by 0.0 audio_scale( captured, 0.0 ); captured_slices = 0; } else { audio_scale( captured, 1.0 / ((Real)captured_slices) ); } } else if ( event.key.keysym.sym == SDLK_BACKSPACE ) { audio_save( captured, "phons/captured.fourier" ); } else if ( event.key.keysym.sym == SDLK_RETURN ) { windowing = !windowing; } else { keypress = 1; break; } } } } SDL_Quit(); return 0; }
int main(int argc, char **argv) { Audio *audio_input = NULL; Features features; #ifdef SUB_RUNNING_AVERAGE Features running_average; Features features_temporary; zero_features( &running_average ); #endif printf("Tiny Speech Recognizer\n"); // Initialize microphone_init(); audio_input = NULL; size_t read_samples; while (1) { audio_free( audio_input ); audio_input = audio_make_buffer( SAMPLES_IN_TIME_SLICE, MIC_FORMAT ); // Read in a slice of audio read_samples = microphone_read( SAMPLES_IN_TIME_SLICE, audio_input ); audio_convert( audio_input, DOUBLE_REAL ); features = features_extract( audio_input ); #ifdef SUB_RUNNING_AVERAGE // First, copy the current feature vector, weighted by tau // We copy these before applying the running average subtraction to get them untainted copy_scaled_features( &features_temporary, &features, ((Real)1.0-(Real)RUNNING_AVERAGE_TAU) ); // Apply the running average subtraction to the current feature vector now that we've saved it subtract_features( &features, &running_average ); // Next, scale the current running average down a little bit scale_features( &running_average, (Real)RUNNING_AVERAGE_TAU ); // Add the saved scaled features into running_average add_features( &running_average, &features_temporary ); #endif #ifdef SQUARE_SUPPRESS square_suppress( &features ); #endif if (above_threshold( &features )) { features_pretty( features ); } //fourier_transform( audio_input, frequencies, SAMPLES_IN_TIME_SLICE ); } // Clean up microphone_deinit(); return 0; }