summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--WasmAudio.xcodeproj/project.pbxproj2
-rw-r--r--WasmAudio/main.c235
2 files changed, 204 insertions, 33 deletions
diff --git a/WasmAudio.xcodeproj/project.pbxproj b/WasmAudio.xcodeproj/project.pbxproj
index 4c87b7c..f4a9f2d 100644
--- a/WasmAudio.xcodeproj/project.pbxproj
+++ b/WasmAudio.xcodeproj/project.pbxproj
@@ -10,7 +10,6 @@
8D1ACB0726A0278200AAF1EB /* firmath.c in Sources */ = {isa = PBXBuildFile; fileRef = 8D1ACB0626A0278200AAF1EB /* firmath.c */; };
8D6E30352694457C007D7ECF /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 8D6E30342694457C007D7ECF /* main.c */; };
8D6E303F269445E3007D7ECF /* SDL2.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8D6E303E269445E2007D7ECF /* SDL2.framework */; };
- 8D6E30442694472D007D7ECF /* Makefile in Sources */ = {isa = PBXBuildFile; fileRef = 8D6E30432694472D007D7ECF /* Makefile */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
@@ -161,7 +160,6 @@
buildActionMask = 2147483647;
files = (
8D1ACB0726A0278200AAF1EB /* firmath.c in Sources */,
- 8D6E30442694472D007D7ECF /* Makefile in Sources */,
8D6E30352694457C007D7ECF /* main.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
diff --git a/WasmAudio/main.c b/WasmAudio/main.c
index 6f1766b..38f5e1a 100644
--- a/WasmAudio/main.c
+++ b/WasmAudio/main.c
@@ -35,12 +35,18 @@
//#include <GLES2/gl2.h>
#endif
+#include "../FIR/firmath.h"
+
#define SCREEN_WIDTH 320
#define SCREEN_WIDTH2 (SCREEN_WIDTH/2)
#define SCREEN_HEIGHT 320
#define SCREEN_HEIGHT2 (SCREEN_HEIGHT/2)
#define BUFSIZE 1536000
+#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)
@@ -54,34 +60,37 @@ static SDL_Renderer *renderer = NULL;
//Audio device IDs
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];
-static double process_buf[BUFSIZE/4];
+const int process_buf_size = BUFSIZE4;
+static double process_buf[process_buf_size*4];
+static double process_buf2[process_buf_size*4];
uint8_t *audio_rec_buffer=NULL;
-int audio_buf_position=0;
-//uint8_t *audio_play_buffer=NULL;
-int current_state=0;
-void audio_recording_callback(void *data, uint8_t *insamples, int len)
-{
- //printf("record len=%d\n",len);
+FILE *f_1,*f_2;
+fir_t fir_lp;
+firw_t firw_lp;
+
+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);
audio_buf_position += len;
}
- //memcpy(insamples, audio_rec_buffer+audio_buf_position, len);
- printf("rec_pos=%d\n",audio_buf_position);
}
void audio_playback_callback(void *data, uint8_t *outsamples, int len) {
- //printf("playback len=%d\n",len);
- //memcpy( audio_rec_buffer,outsamples, len);
+ printf("call play\n");
if (audio_buf_position<audio_rec_buffer_size) {
memcpy( outsamples, audio_rec_buffer+audio_buf_position,len);
audio_buf_position += len;
@@ -101,25 +110,133 @@ enum RecordingState
};
void convert_double_to_f32() {
- int i;
- for (i=0;i<audio_rec_buffer_size;i+=4) {
- float *f_ = (float *)&audio_rec_buffer[i];
- float f = *f_;
- process_buf[i/4] = (double)f;
+ 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];
+ 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<audio_rec_buffer_size;i+=4) {
- double d = process_buf[i/4];
- float f = (float)d;
- float *f_ = (float *)audio_rec_buffer;
- f_[i/4] = (double)f;
+ 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]);
+ }
+
}
+void convert_dobule_up() {
+ int i=0;
+ for (i=0;i<BUFSIZE4;i++) {
+ //process_buf[i] = 0.0;
+ //printf("%f -> ",process_buf[i]);
+ process_buf[i] = process_buf[i]*1.01;
+ if (process_buf[i] > 1.0) process_buf[i] = 1.0;
+ if (process_buf[i] < -1.0) process_buf[i] = -1.0;
+ //printf("%f\n",process_buf[i]);
+ }
+}
+
+void save_to_matlab() {
+ int i;
+ FILE *f=NULL;
+
+ f = fopen("B.mat","w+");
+ fprintf(f,"# Created by me\n");
+ fprintf(f,"# name: B\n");
+ fprintf(f,"# type: matrix\n");
+ fprintf(f,"# rows: 1\n");
+ fprintf(f,"# columns: %d\n",BUFSIZE16);
+
+#if 1
+ for (i=0;i<BUFSIZE16;i++) {
+ float f1=2.0f;
+ fprintf(f," %.4f",process_buf[i]);
+ }
+#endif
+#if 1
+ for (i=0;i<BUFSIZE16;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);
+ //printf(" %.3f",ff);
+
+ }
+#endif
+ fprintf(f,"\n\n\n");
+
+ fclose(f);
+}
+
+#define MIN(x, y) (((x) < (y)) ? (x) : (y))
+void fir_filter(fir_t *fir, double *input, uint32_t ilength, double *output, uint32_t olength) {
+ double acc;
+ double *coeffp;
+ double *inputp;
+ int n;
+ int k;
+
+ memcpy(&input[fir->num_taps-1], input, ilength*sizeof(double));
+
+ for (n=0; n<ilength; n++)
+ {
+ coeffp = fir->fir_coef;
+ inputp = &input[fir->num_taps - 1 + n];
+ acc = 0;
+
+ for (k=0;k<fir->num_taps;k++)
+ {
+ acc += (*coeffp++)*(*inputp--);
+ }
+
+ output[n] = acc;
+ }
+
+ memmove(&input[0], &input[ilength], (fir->num_taps-1)*sizeof(double));
+}
+
uint32_t tick_start, tick_end,tick_1sec;
#if __EMSCRIPTEN__
@@ -127,9 +244,9 @@ void main_tick() {
#else
int main_tick() {
#endif
- int i,j;
+ int i;
int update_fractal=0;
- uint64_t perf_start, perf_end;
+ uint64_t perf_start;
perf_start = SDL_GetPerformanceCounter();
tick_start = SDL_GetTicks();
@@ -180,6 +297,26 @@ int main_tick() {
//process recorderd buffer with low pass filter
if (current_state == RECORDED) {
convert_f32_to_double();
+ save_to_matlab();
+ //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));
+
+#endif
convert_double_to_f32();
}
break;
@@ -209,6 +346,7 @@ int main_tick() {
} else if (current_state == PLAYBACK) {
SDL_LockAudioDevice( playbackDeviceId );
//Finished playback
+ printf("audio_buf_position=%d\n",audio_buf_position);
if( audio_buf_position > audio_rec_buffer_size )
{
//Stop playing audio
@@ -244,9 +382,6 @@ int main_tick() {
tick_end = SDL_GetTicks();
if ((tick_end-tick_1sec)>1000) {
- //perf_end = SDL_GetPerformanceCounter()-perf_start;
- //tick_end = SDL_GetTicks()-tick_start;
- //printf("FPS:%f\n",(1.0f/perf_end));
printf("FPS:%f\n",1.0f/((tick_end-tick_start)/1000.0f));
tick_1sec = tick_end;
}
@@ -260,7 +395,7 @@ int main_tick() {
void main_loop()
{
- int i,count;
+
tick_1sec = SDL_GetTicks();
#if __EMSCRIPTEN__
emscripten_set_main_loop(main_tick, 25, 1);
@@ -271,7 +406,7 @@ void main_loop()
#if !__EMSCRIPTEN__
if (current_state == RECORDING) {
SDL_LockAudioDevice( recordingDeviceId );
- printf("recording=%d\n",audio_buf_position);
+ //printf("recording=%d\n",audio_buf_position);
if( audio_buf_position > audio_rec_buffer_size )
{
//Stop recording audio
@@ -284,6 +419,7 @@ void main_loop()
} else if (current_state == PLAYBACK) {
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 )
{
//Stop playing audio
@@ -299,19 +435,46 @@ void main_loop()
#endif
}
+
+
int main(int argc, const char * argv[]) {
int i,count;
+
+ const int N=200;
+ double h[N],w[N];
+ int filter_N=60;
+ int filter_FS_hz=48000;
+ int filter_FC_hz=20000;
+ double filter_FC=1.0*filter_FC_hz/filter_FS_hz;
+
+
+ fir_lp.num_taps =filter_N;
+ fir_lp.fir_coef = h;
+ firw_lp.num_taps = filter_N;
+ firw_lp.win_coef = w;
+
+ for (i=0;i<N;i++) {
+ h[i] = 0.0;
+ w[i] = 0.0;
+ }
+
+ 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);
+ firw_blackman(&firw_lp);
+ //firw_rect(&firw_lp);
+ fir_convolute(&fir_lp, &firw_lp);
+ fir_print_matlab(&fir_lp);
+
// insert code here...
printf("Hello, World2!\n");
- //if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 )
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "EXIT:Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
-
-
atexit(SDL_Quit);
printf("Prepare SDL2 window\n");
@@ -356,6 +519,10 @@ int main(int argc, const char * argv[]) {
printf("Format changed %d -> %d\n",audio_rec_want.format, audio_rec_have.format);
}
+ printf("F32 is signed (%d)\n",SDL_AUDIO_ISSIGNED(audio_rec_have.format));
+ printf("F32 is bigend (%d)\n",SDL_AUDIO_ISBIGENDIAN(audio_rec_have.format));
+ printf("F32 is bitsize(%d)\n",SDL_AUDIO_BITSIZE(audio_rec_have.format));
+
if (recordingDeviceId == 0) {
printf("Failed to open recording device\n");
} else {
@@ -414,6 +581,9 @@ int main(int argc, const char * argv[]) {
}
*/
+ f_1 = fopen("pre.txt","w+");
+ f_2 = fopen("post.txt","w+");
+
main_loop();
SDL_DestroyRenderer(renderer);
@@ -422,5 +592,8 @@ int main(int argc, const char * argv[]) {
SDL_VideoQuit();//?
SDL_Quit();
+ fclose(f_1);
+ fclose(f_2);
+
return 0;
}