diff options
author | ZoRo <dos21h@gmail.com> | 2021-07-30 09:22:29 +0100 |
---|---|---|
committer | ZoRo <dos21h@gmail.com> | 2021-07-30 09:22:29 +0100 |
commit | 08988d7ad00709b0ff7673b01150e3954f2c96bc (patch) | |
tree | 564c0dd665c8a36b4f18515b0621f3cedd41ba54 /WasmAudio | |
parent | 8d015dd123cd6a8d16e69283ef3616103a2b5fcf (diff) | |
download | WasmAudio-08988d7ad00709b0ff7673b01150e3954f2c96bc.tar.gz WasmAudio-08988d7ad00709b0ff7673b01150e3954f2c96bc.zip |
Attempt to fix noise at the end of record
Diffstat (limited to 'WasmAudio')
-rw-r--r-- | WasmAudio/main.c | 51 |
1 files changed, 33 insertions, 18 deletions
diff --git a/WasmAudio/main.c b/WasmAudio/main.c index 6ee9f3b..4cb9f2e 100644 --- a/WasmAudio/main.c +++ b/WasmAudio/main.c @@ -49,16 +49,24 @@ #define SCREEN_HEIGHT 320 #define SCREEN_HEIGHT2 (SCREEN_HEIGHT/2) -#define BUFSIZE 1536000 + +//Maximum recording time plus padding +#define RECORDING_BUFFER_SECONDS (2) +//Maximum recording time +#define MAX_RECORDING_SECONDS (RECORDING_BUFFER_SECONDS) +#define RECORDING_BUFFER_CHANNELS (2) +#define RECORDING_BUFFER_SAMPLERATE (48000) +#define RECORDING_BUFFER_SAMPLESIZE (4) //LSB float32 +#define RECORDING_BUFFER_SAMPLES (4096) +#define SDL_CALLBACK_USED_BUFSIZE (RECORDING_BUFFER_SAMPLES*RECORDING_BUFFER_SAMPLESIZE*RECORDING_BUFFER_CHANNELS ) + +#define BUFSIZE (RECORDING_BUFFER_SECONDS*RECORDING_BUFFER_CHANNELS*RECORDING_BUFFER_SAMPLERATE*RECORDING_BUFFER_SAMPLESIZE) #define BUFSIZE2 (BUFSIZE/2) #define BUFSIZE4 (BUFSIZE/4) #define BUFSIZE8 (BUFSIZE/8) #define BUFSIZE16 (BUFSIZE/16) -//Maximum recording time plus padding -#define RECORDING_BUFFER_SECONDS (2) -//Maximum recording time -#define MAX_RECORDING_SECONDS (RECORDING_BUFFER_SECONDS+2) + //SDL drawing recording int first_run=1; @@ -71,15 +79,6 @@ static SDL_AudioDeviceID playbackDeviceId = 0; int audio_buf_position=0; int current_state=0; -//audio buffer vars -int audio_rec_buffer_size=-1; -int audio_rec_buffer_max_size=-1; -static uint8_t audio_rec_buffer_[BUFSIZE]; -#define PROCESS_BUF_SIZE (BUFSIZE4) -static double process_buf[PROCESS_BUF_SIZE+100]; -static double process_buf2[PROCESS_BUF_SIZE+100]; -uint8_t *audio_rec_buffer=NULL; - //FIR filter stuff FILE *f_1,*f_2; fir_t fir_lp; @@ -92,6 +91,18 @@ int filter_FS_hz=48000; int filter_FC_hz=1000; double filter_FC; +//audio buffer vars +int audio_rec_buffer_size=-1; +int audio_rec_buffer_max_size=-1; +static uint8_t audio_rec_buffer_[BUFSIZE]; +#define PROCESS_BUF_SIZE (BUFSIZE) +uint8_t *audio_rec_buffer=NULL; +static double process_buf[PROCESS_BUF_SIZE+200]; +static double process_buf2[PROCESS_BUF_SIZE+200]; + + + + void audio_recording_callback(void *data, uint8_t *insamples, int len) { if (audio_buf_position<audio_rec_buffer_size) { memcpy( audio_rec_buffer+audio_buf_position, insamples, len); @@ -101,8 +112,8 @@ void audio_recording_callback(void *data, uint8_t *insamples, int len) { } void audio_playback_callback(void *data, uint8_t *outsamples, int len) { - //printf("call play\n"); - if (audio_buf_position<audio_rec_buffer_size) { + printf("call play %d\n",audio_buf_position); + if (audio_buf_position<=(audio_rec_buffer_size-SDL_CALLBACK_USED_BUFSIZE)) { memcpy( outsamples, audio_rec_buffer+audio_buf_position,len); audio_buf_position += len; } else { @@ -303,7 +314,7 @@ void save_to_matlab(char *name) { for (i=0;i<dumpSize;i++) { float f1=2.0f; fprintf(f," %.4f",process_buf[i]); - printf(" %.4f",process_buf[i]); + //printf(" %.4f",process_buf[i]); } #endif #if 0 @@ -344,6 +355,7 @@ void fir_filter(fir_t *fir, double *input, uint32_t ilength, double *output, uin output[n] = acc; } + printf("max n value %d\n",n); memmove(&input[0], &input[ilength-fir->num_taps], (fir->num_taps-1)*sizeof(double)); } @@ -564,7 +576,7 @@ void main_loop() SDL_LockAudioDevice( playbackDeviceId ); //Finished playback //printf("audio_buf_position=%d audio_rec_buffer_size=%d\n",audio_buf_position,audio_rec_buffer_size); - if( audio_buf_position > audio_rec_buffer_size ) + if( audio_buf_position >= (audio_rec_buffer_size-SDL_CALLBACK_USED_BUFSIZE) ) { //Stop playing audio SDL_PauseAudioDevice( playbackDeviceId, SDL_TRUE ); @@ -654,6 +666,7 @@ int main(int argc, const char * argv[]) { audio_rec_want.format = AUDIO_F32; audio_rec_want.channels = 2; audio_rec_want.samples = 4096; + //audio_rec_want.silence = 0; audio_rec_want.callback = audio_recording_callback; audio_rec_device_name = SDL_GetAudioDeviceName(0, SDL_TRUE ); @@ -684,6 +697,7 @@ int main(int argc, const char * argv[]) { audio_rec_buffer = &audio_rec_buffer_[0]; printf("buffsize_max=%d\n",audio_rec_buffer_max_size); printf("buffsize=%d\n",audio_rec_buffer_size); + printf("allocated=%d\n",BUFSIZE); memset(audio_rec_buffer,0,audio_rec_buffer_max_size); } @@ -692,6 +706,7 @@ int main(int argc, const char * argv[]) { audio_play_want.format = AUDIO_F32; audio_play_want.channels = 2; audio_play_want.samples = 4096; + //audio_play_want.silence = 0; audio_play_want.callback = audio_playback_callback; playbackDeviceId = SDL_OpenAudioDevice(NULL, SDL_FALSE, &audio_play_want, &audio_play_have, SDL_AUDIO_ALLOW_FORMAT_CHANGE|SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); |