summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArturs Artamonovs <arturs.artamonovs@protonmail.com>2021-07-22 09:40:38 +0100
committerArturs Artamonovs <arturs.artamonovs@protonmail.com>2021-07-22 09:40:38 +0100
commite4619445203c9b17788cbcedeac4f4918b2ac2b3 (patch)
treea8c5eb816b1c40c1c0c0a40373ad3c34b44f61cf
parentc21a3f1a17a4ef894ca105b8679daac27293096a (diff)
downloadWasmAudio-e4619445203c9b17788cbcedeac4f4918b2ac2b3.tar.gz
WasmAudio-e4619445203c9b17788cbcedeac4f4918b2ac2b3.zip
clean the code
-rw-r--r--WasmAudio/main.c110
1 files changed, 31 insertions, 79 deletions
diff --git a/WasmAudio/main.c b/WasmAudio/main.c
index 38f5e1a..2ab9449 100644
--- a/WasmAudio/main.c
+++ b/WasmAudio/main.c
@@ -61,18 +61,15 @@ static SDL_Renderer *renderer = NULL;
static SDL_AudioDeviceID recordingDeviceId = 0;
static SDL_AudioDeviceID playbackDeviceId = 0;
int audio_buf_position=0;
-//uint8_t *audio_play_buffer=NULL;
int current_state=0;
int audio_rec_buffer_size=-1;
int audio_rec_buffer_max_size=-1;
-//int audio_play_buffer_size=-1;
-//int audio_play_buffer_max_size=-1;
static uint8_t audio_rec_buffer_[BUFSIZE];
const int process_buf_size = BUFSIZE4;
-static double process_buf[process_buf_size*4];
-static double process_buf2[process_buf_size*4];
+static double process_buf[process_buf_size+100];
+static double process_buf2[process_buf_size+100];
uint8_t *audio_rec_buffer=NULL;
@@ -111,56 +108,42 @@ enum RecordingState
void convert_double_to_f32() {
int i=0;
- printf("float sizeof=%lu\n",sizeof(float));
- printf("double sizeof=%lu\n",sizeof(double));
for (i=0;i<BUFSIZE2;i+=4) {
double d = process_buf[i/4];
+ if (i/4 > BUFSIZE4) {
+ printf("Buffer overflow convert_double_to_f32");
+ exit(1);
+ }
float f = d;
- //printf("%f ",f);
- //f = f*2147483647.0;
int32_t *i32_ = &f,i32=*i32_;
- //printf("%d %f\n",i32,f);
-
- //audio_rec_buffer[i+0] = ((int)i32>> 24)&0xff;
- //audio_rec_buffer[i+1] = ((int)i32>> 16)&0xff;
- //audio_rec_buffer[i+2] = ((int)i32>> 8)&0xff;
- //audio_rec_buffer[i+3] = ((int)i32&0xff);
-
audio_rec_buffer[i+3] = ((int)i32>> 24)&0xff;
audio_rec_buffer[i+2] = ((int)i32>> 16)&0xff;
audio_rec_buffer[i+1] = ((int)i32>> 8)&0xff;
audio_rec_buffer[i+0] = ((int)i32&0xff);
-
- //audio_rec_buffer[i+0] = ((int)f>> 24)&0xff;
- //audio_rec_buffer[i+1] = ((int)f>> 16)&0xff;
- //audio_rec_buffer[i+2] = ((int)f>> 8)&0xff;
- //audio_rec_buffer[i+3] = ((int)f&0xff);
-
- //memcpy(&f,&audio_rec_buffer[i],4);
+
}
//printf("\n");
- for (i=0;i<100;i+=4) {
- fprintf(f_2, "%d %02x %02x %02x %02x\n", i/4,audio_rec_buffer[i],audio_rec_buffer[i+1],audio_rec_buffer[i+2],audio_rec_buffer[i+3]);
- }
}
void convert_f32_to_double() {
int i=0;
- for (i=0;i<100;i+=4) {
- fprintf(f_1, "%d %02x %02x %02x %02x\n", i/4,audio_rec_buffer[i],audio_rec_buffer[i+1],audio_rec_buffer[i+2],audio_rec_buffer[i+3]);
- }
for (i=0;i<BUFSIZE2;i+=4) {
int32_t i32 = ((audio_rec_buffer[i+3] << 24) | (audio_rec_buffer[i+2] << 16) | (audio_rec_buffer[i+1] << 8) | (audio_rec_buffer[i+0]) );
float *ff = &i32;
//double d = f;
- process_buf[i/4] = *ff;
- printf("%f %f\n",ff,process_buf[i/4]);
+ if ((i/4)<BUFSIZE4) {
+ process_buf[i/4] = *ff;
+ } else {
+ printf("Buffer overflow convert_f32_to_double\n");
+ exit(0);
+ }
+ //printf("%f %f\n",ff,process_buf[i/4]);
}
}
@@ -178,29 +161,31 @@ void convert_dobule_up() {
}
}
-void save_to_matlab() {
+void save_to_matlab(char *name) {
int i;
FILE *f=NULL;
-
- f = fopen("B.mat","w+");
+ char namebuf[256];
+ snprintf(namebuf,sizeof(namebuf),"%s.mat",name);
+ f = fopen(namebuf,"w+");
fprintf(f,"# Created by me\n");
- fprintf(f,"# name: B\n");
+ fprintf(f,"# name: %s\n",name);
fprintf(f,"# type: matrix\n");
fprintf(f,"# rows: 1\n");
- fprintf(f,"# columns: %d\n",BUFSIZE16);
+ int dumpSize=BUFSIZE16;
+ fprintf(f,"# columns: %d\n",dumpSize);
#if 1
- for (i=0;i<BUFSIZE16;i++) {
+ for (i=0;i<dumpSize;i++) {
float f1=2.0f;
fprintf(f," %.4f",process_buf[i]);
}
#endif
-#if 1
- for (i=0;i<BUFSIZE16;i+=4) {
+#if 0
+ for (i=0;i<dumpSize;i+=4) {
int32_t i32 = (float)((audio_rec_buffer[i+0] << 24) | (audio_rec_buffer[i+1] << 16) | (audio_rec_buffer[i+2] << 8) | (audio_rec_buffer[i+3]) );
float ff=i32;
- //fprintf(f," %.3f",ff);
+ fprintf(f," %.3f",ff);
//printf(" %.3f",ff);
}
@@ -297,25 +282,17 @@ int main_tick() {
//process recorderd buffer with low pass filter
if (current_state == RECORDED) {
convert_f32_to_double();
- save_to_matlab();
+ save_to_matlab("B1");
//simple volume raise
#if 0
convert_dobule_up();
#endif
//fir filter apply
#if 1
- printf("Apply low pass filter\n");
-
+
fir_filter(&fir_lp, process_buf, process_buf_size, process_buf2, process_buf_size);
- //memcpy(process_buf2,process_buf,process_buf_size*sizeof(double));
memcpy(process_buf,process_buf2,process_buf_size*sizeof(double));
- //for (i=0;i<100;i++) {
- //process_buf[i] = process_buf2[i];
- // printf("[%d]=%f\n",i,process_buf[i]);
- //}
-
- //memmove(process_buf,process_buf2,process_buf_size*sizeof(double));
-
+ save_to_matlab("B2");
#endif
convert_double_to_f32();
}
@@ -444,7 +421,7 @@ int main(int argc, const char * argv[]) {
double h[N],w[N];
int filter_N=60;
int filter_FS_hz=48000;
- int filter_FC_hz=20000;
+ int filter_FC_hz=1000;
double filter_FC=1.0*filter_FC_hz/filter_FS_hz;
@@ -460,8 +437,8 @@ int main(int argc, const char * argv[]) {
fir_init(&fir_lp, filter_N);
firw_init(&firw_lp, filter_N);
- //fir_sinc_lp(&fir_lp, filter_FC);
- fir_sinc_hp(&fir_lp, filter_FC);
+ fir_sinc_lp(&fir_lp, filter_FC);
+ //fir_sinc_hp(&fir_lp, filter_FC);
firw_blackman(&firw_lp);
//firw_rect(&firw_lp);
fir_convolute(&fir_lp, &firw_lp);
@@ -556,31 +533,6 @@ int main(int argc, const char * argv[]) {
current_state = STOPPED;
printf("State: STOPED\n");
-
-
-
-
- //SDL_PauseAudioDevice(recordingDeviceId,0);
- //!SDL_LockAudioDevice(recordingDeviceId);
- //SDL_PauseAudioDevice(recordingDeviceId, 0);
- //SDL_PauseAudio(0);
- //SDL_Delay(4000);
- //!SDL_UnlockAudioDevice(recordingDeviceId);
- //!SDL_CloseAudio();
- //SDL_CloseAudioDevice(recordingDeviceId);
- /*
- if (SDL_OpenAudio(&audio_want, &audio_have) < 0) {
- SDL_Log("Failed to open audio: %s", SDL_GetError());
- } else {
- if (audio_have.format != audio_want.format) {
- SDL_Log("We didn't get Float32 audio format.");
- }
- SDL_PauseAudio(0);
- SDL_Delay(5000);
- SDL_CloseAudio();
- }
- */
-
f_1 = fopen("pre.txt","w+");
f_2 = fopen("post.txt","w+");