Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ int write_pointer = 0;
uint64_t timestamp = 0;
pthread_mutex_t audio_mutex;

/* Audio input stream object */
static PaStream *stream;

/* Data for PA callback to use */
static struct callback_info {
int channels; //!< Number of channels
Expand Down Expand Up @@ -88,10 +91,8 @@ static int paudio_callback(const void *input_buffer,
return 0;
}

int start_portaudio(int *nominal_sample_rate, double *real_sample_rate)
int start_portaudio(int *nominal_sample_rate, double *real_sample_rate, bool light)
{
PaStream *stream;

if(pthread_mutex_init(&audio_mutex,NULL)) {
error("Failed to setup audio mutex");
return 1;
Expand Down Expand Up @@ -121,7 +122,7 @@ int start_portaudio(int *nominal_sample_rate, double *real_sample_rate)
}
if(channels > 2) channels = 2;
info.channels = channels;
info.light = false;
info.light = light;
err = Pa_OpenDefaultStream(&stream,channels,0,paFloat32,PA_SAMPLE_RATE,paFramesPerBufferUnspecified,paudio_callback,&info);
if(err!=paNoError)
goto error;
Expand Down Expand Up @@ -235,11 +236,18 @@ int analyze_pa_data_cal(struct processing_data *pd, struct calibration_data *cd)
void set_audio_light(bool light)
{
if(info.light != light) {
Pa_StopStream(stream);
pthread_mutex_lock(&audio_mutex);

info.light = light;
memset(pa_buffers, 0, sizeof(pa_buffers));
write_pointer = 0;
timestamp = 0;

pthread_mutex_unlock(&audio_mutex);

PaError err = Pa_StartStream(stream);
if(err != paNoError)
error("Error re-starting audio input: %s", Pa_GetErrorText(err));
}
}
10 changes: 5 additions & 5 deletions src/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -917,11 +917,6 @@ static void start_interface(GApplication* app, void *p)

struct main_window *w = malloc(sizeof(struct main_window));

if(start_portaudio(&w->nominal_sr, &real_sr)) {
g_application_quit(app);
return;
}

w->app = GTK_APPLICATION(app);

w->zombie = 0;
Expand All @@ -934,6 +929,11 @@ static void start_interface(GApplication* app, void *p)

load_config(w);

if(start_portaudio(&w->nominal_sr, &real_sr, w->is_light)) {
g_application_quit(app);
return;
}

if(w->la < MIN_LA || w->la > MAX_LA) w->la = DEFAULT_LA;
if(w->bph < MIN_BPH || w->bph > MAX_BPH) w->bph = 0;
if(w->cal < MIN_CAL || w->cal > MAX_CAL)
Expand Down
2 changes: 1 addition & 1 deletion src/tg.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ struct processing_data {
int is_light;
};

int start_portaudio(int *nominal_sample_rate, double *real_sample_rate);
int start_portaudio(int *nominal_sample_rate, double *real_sample_rate, bool light);
int terminate_portaudio();
uint64_t get_timestamp(int light);
int analyze_pa_data(struct processing_data *pd, int bph, double la, uint64_t events_from);
Expand Down