summaryrefslogtreecommitdiff
path: root/WasmAudio/main.c
diff options
context:
space:
mode:
authorArturs Artamonovs <arturs.artamonovs@protonmail.com>2021-07-30 09:39:22 +0100
committerArturs Artamonovs <arturs.artamonovs@protonmail.com>2021-07-30 09:39:22 +0100
commita402265b87c89ca3ab73c49fb1575b7b8f6ac04d (patch)
treeb307daa00fbf21c1c4ec57f99573ab4b27f9e6f6 /WasmAudio/main.c
parent08988d7ad00709b0ff7673b01150e3954f2c96bc (diff)
downloadWasmAudio-a402265b87c89ca3ab73c49fb1575b7b8f6ac04d.tar.gz
WasmAudio-a402265b87c89ca3ab73c49fb1575b7b8f6ac04d.zip
Fixed few glitchesmain
Diffstat (limited to 'WasmAudio/main.c')
-rw-r--r--WasmAudio/main.c28
1 files changed, 17 insertions, 11 deletions
diff --git a/WasmAudio/main.c b/WasmAudio/main.c
index 4cb9f2e..86d307f 100644
--- a/WasmAudio/main.c
+++ b/WasmAudio/main.c
@@ -51,7 +51,7 @@
//Maximum recording time plus padding
-#define RECORDING_BUFFER_SECONDS (2)
+#define RECORDING_BUFFER_SECONDS (1)
//Maximum recording time
#define MAX_RECORDING_SECONDS (RECORDING_BUFFER_SECONDS)
#define RECORDING_BUFFER_CHANNELS (2)
@@ -78,6 +78,7 @@ static SDL_AudioDeviceID recordingDeviceId = 0;
static SDL_AudioDeviceID playbackDeviceId = 0;
int audio_buf_position=0;
int current_state=0;
+int audio_buf_full=0;
//FIR filter stuff
FILE *f_1,*f_2;
@@ -97,27 +98,30 @@ 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];
+static double process_buf[PROCESS_BUF_SIZE];
+static double process_buf2[PROCESS_BUF_SIZE];
void audio_recording_callback(void *data, uint8_t *insamples, int len) {
- if (audio_buf_position<audio_rec_buffer_size) {
+ if (audio_buf_position+len<audio_rec_buffer_size) {
memcpy( audio_rec_buffer+audio_buf_position, insamples, len);
audio_buf_position += len;
+ } else {
+ audio_buf_full = 1;
}
}
void audio_playback_callback(void *data, uint8_t *outsamples, int len) {
printf("call play %d\n",audio_buf_position);
- if (audio_buf_position<=(audio_rec_buffer_size-SDL_CALLBACK_USED_BUFSIZE)) {
+ if (audio_buf_position+len <(audio_rec_buffer_size)) {
memcpy( outsamples, audio_rec_buffer+audio_buf_position,len);
audio_buf_position += len;
} else {
memset( outsamples, 0, len*sizeof(double));
+ audio_buf_full = 1;
}
}
@@ -136,7 +140,7 @@ enum RecordingState
void convert_double_to_f32() {
int i=0;
- for (i=0;i<BUFSIZE2;i+=4) {
+ for (i=0;i<BUFSIZE;i+=4) {
double d = process_buf[i/4];
if (i/4 > BUFSIZE4) {
printf("Buffer overflow convert_double_to_f32");
@@ -158,7 +162,7 @@ void convert_double_to_f32() {
void convert_f32_to_double() {
int i=0;
- for (i=0;i<BUFSIZE2;i+=4) {
+ for (i=0;i<BUFSIZE;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;
@@ -432,6 +436,7 @@ int main_tick() {
//record if stopped
if ((current_state == STOPPED) || (current_state == RECORDED)) {
audio_buf_position = 0;
+ audio_buf_full = 0;
SDL_PauseAudioDevice(recordingDeviceId, SDL_FALSE);
current_state = RECORDING;
printf("State: RECORDING\n");
@@ -443,6 +448,7 @@ int main_tick() {
if (current_state == RECORDED) {
audio_buf_position = 0;
SDL_PauseAudioDevice( playbackDeviceId, SDL_FALSE );
+ audio_buf_full = 0;
current_state = PLAYBACK;
printf("State: PLAYBACK\n");
} /*else if (current_state == PLAYBACK) {
@@ -487,7 +493,7 @@ int main_tick() {
if (current_state == RECORDING) {
SDL_LockAudioDevice( recordingDeviceId );
printf("recording=%d\n",audio_buf_position);
- if( audio_buf_position > audio_rec_buffer_size )
+ if( (audio_buf_position > audio_rec_buffer_size) || (audio_buf_full) )
{
//Stop recording audio
SDL_PauseAudioDevice( recordingDeviceId, SDL_TRUE );
@@ -500,7 +506,7 @@ int main_tick() {
SDL_LockAudioDevice( playbackDeviceId );
//Finished playback
printf("audio_buf_position=%d\n",audio_buf_position);
- if( audio_buf_position > audio_rec_buffer_size )
+ if( (audio_buf_position > audio_rec_buffer_size) || (audio_buf_full) )
{
//Stop playing audio
//SDL_Delay(500);
@@ -563,7 +569,7 @@ void main_loop()
if (current_state == RECORDING) {
SDL_LockAudioDevice( recordingDeviceId );
//printf("recording=%d\n",audio_buf_position);
- if( audio_buf_position > audio_rec_buffer_size )
+ if( (audio_buf_position > audio_rec_buffer_size) || (audio_buf_full) )
{
//Stop recording audio
SDL_PauseAudioDevice( recordingDeviceId, SDL_TRUE );
@@ -576,7 +582,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-SDL_CALLBACK_USED_BUFSIZE) )
+ if( (audio_buf_position >= (audio_rec_buffer_size-SDL_CALLBACK_USED_BUFSIZE)) || (audio_buf_full) )
{
//Stop playing audio
SDL_PauseAudioDevice( playbackDeviceId, SDL_TRUE );