void LPComplexODF::init() { coefs = new sample[order]; num_bins = (frame_size/2) + 1; if(window) delete [] window; window = new sample[frame_size]; for(int i = 0; i < frame_size; i++) { window[i] = 1.0; } hann_window(frame_size, window); distances = new sample*[num_bins]; for(int i = 0; i < num_bins; i++) { distances[i] = new sample[order]; for(int j = 0; j < order; j++) { distances[i][j] = 0.0; } } prev_frame = new fftw_complex[num_bins]; for(int i = 0; i < num_bins; i++) { prev_frame[i][0] = 0.0; prev_frame[i][1] = 0.0; } in = (sample*) fftw_malloc(sizeof(sample) * frame_size); out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * num_bins); p = fftw_plan_dft_r2c_1d(frame_size, in, out, FFTW_ESTIMATE); }
void SpectralDifferenceODF::reset() { num_bins = (frame_size/2) + 1; if(window) delete [] window; window = new sample[frame_size]; for(int i = 0; i < frame_size; i++) { window[i] = 1.0; } hann_window(frame_size, window); if(prev_amps) delete [] prev_amps; prev_amps = new sample[num_bins]; for(int i = 0; i < num_bins; i++) { prev_amps[i] = 0; } if(in) fftw_free(in); in = (sample*) fftw_malloc(sizeof(sample) * frame_size); if(out) fftw_free(out); out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * num_bins); fftw_destroy_plan(p); p = fftw_plan_dft_r2c_1d(frame_size, in, out, FFTW_ESTIMATE); }
/** * @brief chroma_compute * @param c Accord à initialiser * @param samples Tableau des samples * @param sample_size Taille du tableau d'échantillonage * @return 1 en cas d'erreur, 0 sinon. * * Analyse de samples. */ int chroma_compute(chord_ctrl *c, double *samples, int sample_size) { /* Verif*/ if (sample_size <= 0) { printf("Error chord.c : sample_size"); return 1; } if (!samples) { printf("Error chord.c : samples"); return 1; } if (!c) { printf("Error chord.c : c"); return 1; } /* ProcessFFT */ complex spectrum[ZERO_SIZE]; double in[ZERO_SIZE]; // printf("taille: %d\n", sizeof(double)); int i; for (i= 0; i < sample_size; i++) in[i] = samples[i] * hann_window(i, sample_size); for (i= sample_size; i < ZERO_SIZE; i++) in[i] = 0.0; fft(in, ZERO_SIZE, spectrum); /* Compute EHPCP */ if (!compute_EHPCP(spectrum, c->chroma, ZERO_SIZE, CHROMA_SIZE, c->samplerate)) return 0; c->num_frames++; c->total_samples += sample_size; return 0; }
int init_mq(MQParameters* params) { /* TODO: check memory allocation */ /* allocate memory for window */ params->window = (sample*) malloc(sizeof(sample) * params->frame_size); int i; for(i = 0; i < params->frame_size; i++) { params->window[i] = 1.0; } hann_window(params->frame_size, params->window); /* allocate memory for FFT */ params->fft_in = (sample*) fftw_malloc(sizeof(sample) * params->frame_size); params->fft_out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * params->num_bins); params->fft_plan = fftw_plan_dft_r2c_1d(params->frame_size, params->fft_in, params->fft_out, FFTW_ESTIMATE); /* set other variables to defaults */ reset_mq(params); return 0; }
int init_mq(MQParameters* params) { // allocate memory for window params->window = (sample*) malloc(sizeof(sample) * params->frame_size); int i; for(i = 0; i < params->frame_size; i++) { params->window[i] = 1.0; } hann_window(params->frame_size, params->window); // allocate memory for FFT params->fft_in = (sample*) fftw_malloc(sizeof(sample) * params->frame_size); params->fft_out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * params->num_bins); params->fft_plan = fftw_plan_dft_r2c_1d(params->frame_size, params->fft_in, params->fft_out, FFTW_ESTIMATE); // set other variables to defaults params->prev_peaks = NULL; params->prev_peaks2 = NULL; reset_mq(params); return 0; }
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; }