Skip to content

Commit

Permalink
added mutex lock on write to fftw input buffers and fftw execute
Browse files Browse the repository at this point in the history
this will prevent an fftw execute from taking place while data is
beeing written to buffers
  • Loading branch information
karlstav committed Sep 7, 2020
1 parent f5b2b79 commit 1a75b17
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 24 deletions.
6 changes: 6 additions & 0 deletions cava.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
// struct termios oldtio, newtio;
// int M = 8 * 1024;

pthread_mutex_t lock;
// used by sig handler
// needs to know output mode in order to clean up terminal
int output_mode;
Expand Down Expand Up @@ -875,12 +876,14 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co

// process: execute FFT and sort frequency bands
if (p.stereo) {
pthread_mutex_lock(&lock);
fftw_execute(p_bass_l);
fftw_execute(p_bass_r);
fftw_execute(p_mid_l);
fftw_execute(p_mid_r);
fftw_execute(p_treble_l);
fftw_execute(p_treble_r);
pthread_mutex_unlock(&lock);

bars_left = separate_freq_bands(
audio.FFTbassbufferSize, out_bass_l, audio.FFTmidbufferSize, out_mid_l,
Expand All @@ -895,9 +898,12 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
FFTbuffer_upper_cut_off, eq, RIGHT_CHANNEL, p.sens, p.ignore);

} else {
pthread_mutex_lock(&lock);
fftw_execute(p_bass_l);
fftw_execute(p_mid_l);
fftw_execute(p_treble_l);
pthread_mutex_unlock(&lock);

bars_mono = separate_freq_bands(
audio.FFTbassbufferSize, out_bass_l, audio.FFTmidbufferSize, out_mid_l,
audio.FFTtreblebufferSize, out_treble_l, bass_cut_off_bar,
Expand Down
4 changes: 3 additions & 1 deletion input/alsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ void *input_alsa(void *data) {
debug("short read, read %d %d frames\n", err, (int)frames);
}

write_to_fftw_input_buffers(buf, frames, data);
pthread_mutex_lock(&lock);
write_to_fftw_input_buffers(frames, buf, data);
pthread_mutex_unlock(&lock);
}

free(buffer);
Expand Down
2 changes: 1 addition & 1 deletion input/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void reset_output_buffers(struct audio_data *data) {
memset(data->in_treble_l, 0, sizeof(double) * 2 * (data->FFTtreblebufferSize / 2 + 1));
}

int write_to_fftw_input_buffers(int16_t buf[], int16_t frames, void *data) {
int write_to_fftw_input_buffers(int16_t frames, int16_t buf[frames * 2], void *data) {
struct audio_data *audio = (struct audio_data *)data;

for (uint16_t i = 0; i < frames * 2; i += 2) {
Expand Down
4 changes: 3 additions & 1 deletion input/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ struct audio_data {

void reset_output_buffers(struct audio_data *data);

int write_to_fftw_input_buffers(int16_t buf[], int16_t frames, void *data);
int write_to_fftw_input_buffers(int16_t frames, int16_t buf[frames * 2], void *data);

extern pthread_mutex_t lock;
4 changes: 3 additions & 1 deletion input/fifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ void *input_fifo(void *data) {

// We worked with unsigned ints up until now to save on sign extension, but the FFT wants
// signed ints.
write_to_fftw_input_buffers((int16_t *)samples, SAMPLES_PER_BUFFER / 2, audio);
pthread_mutex_lock(&lock);
write_to_fftw_input_buffers(SAMPLES_PER_BUFFER / 2, (int16_t *)samples, audio);
pthread_mutex_unlock(&lock);
}

close(fd);
Expand Down
6 changes: 4 additions & 2 deletions input/portaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ static int recordCallback(const void *inputBuffer, void *outputBuffer,
}

if (inputBuffer == NULL) {
write_to_fftw_input_buffers(silence_buffer, framesToCalc, audio);
pthread_mutex_lock(&lock);
write_to_fftw_input_buffers(framesToCalc, silence_buffer, audio);
pthread_mutex_unlock(&lock);
/*
for(i=0; i<framesToCalc; i++) {
if(audio->channels == 1) audio->audio_out_l[n] = SAMPLE_SILENCE;
Expand All @@ -53,7 +55,7 @@ static int recordCallback(const void *inputBuffer, void *outputBuffer,
}
*/
} else {
write_to_fftw_input_buffers(rptr, framesToCalc, audio);
write_to_fftw_input_buffers(framesToCalc, rptr, audio);
/*
for(i=0; i<framesToCalc; i++) {
if(audio->channels == 1) {
Expand Down
6 changes: 3 additions & 3 deletions input/pulse.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ void *input_pulse(void *data) {
audio->terminate = 1;
}

// sorting out channels

write_to_fftw_input_buffers(buf, frames, data);
pthread_mutex_lock(&lock);
write_to_fftw_input_buffers(frames, buf, data);
pthread_mutex_unlock(&lock);
}

pa_simple_free(s);
Expand Down
6 changes: 4 additions & 2 deletions input/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ void *input_shmem(void *data) {
// Thus, the starting position only affects the phase spectrum of the
// fft, and not the power spectrum, so we can just read in the
// whole buffer.
write_to_fftw_input_buffers(mmap_area->buffer, buf_frames, audio);
pthread_mutex_lock(&lock);
write_to_fftw_input_buffers(buf_frames, mmap_area->buffer, audio);
pthread_mutex_unlock(&lock);
nanosleep(&req, NULL);
} else {
write_to_fftw_input_buffers(silence_buffer, buf_frames, audio);
write_to_fftw_input_buffers(buf_frames, silence_buffer, audio);
nanosleep(&req, NULL);
}
}
Expand Down
16 changes: 3 additions & 13 deletions input/sndio.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,9 @@ void *input_sndio(void *data) {
exit(EXIT_FAILURE);
}

write_to_fftw_input_buffers(buf, frames, audio);
/*
for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i += 2) {
if (par.rchan == 1) {
// sndiod has already taken care of averaging the samples
audio->audio_out_l[n] = buf[i];
} else if (par.rchan == 2) {
audio->audio_out_l[n] = buf[i];
audio->audio_out_r[n] = buf[i + 1];
}
n = (n + 1) % audio->FFTbufferSize;
}
*/
pthread_mutex_lock(&lock);
write_to_fftw_input_buffers(frames, buf, audio);
pthread_mutex_unlock(&lock);
}

sio_stop(hdl);
Expand Down

0 comments on commit 1a75b17

Please sign in to comment.