Skip to content

Commit

Permalink
Use a pthread mutex around the volume settings
Browse files Browse the repository at this point in the history
Using the `volatile` qualifier in multithreading code is never the right
answer. Mutexes should be used as was attempted with the audio buffer
code. Here, we implement a new mutex for the volume and fix_volume
globals, and grab a lock on it when necessary, which is for both reads
and writes.
  • Loading branch information
toofishes committed Feb 24, 2012
1 parent 0a446a4 commit 8b1728d
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions hairtunes.c
Expand Up @@ -97,9 +97,10 @@ static void rtp_request_resend(seq_t first, seq_t last);
static void ab_resync(void); static void ab_resync(void);


// interthread variables // interthread variables
// stdin->decoder // stdin->decoder
volatile double volume = 1.0; static double volume = 1.0;
volatile long fix_volume = 0x10000; static int fix_volume = 0x10000;
static pthread_mutex_t vol_mutex = PTHREAD_MUTEX_INITIALIZER;


typedef struct audio_buffer_entry { // decoded audio packets typedef struct audio_buffer_entry { // decoded audio packets
int ready; int ready;
Expand Down Expand Up @@ -220,8 +221,10 @@ int hairtunes_init(char *pAeskey, char *pAesiv, char *fmtpstr, int pCtrlPort, in
assert(f<=0); assert(f<=0);
if (debug) if (debug)
fprintf(stderr, "VOL: %lf\n", f); fprintf(stderr, "VOL: %lf\n", f);
pthread_mutex_lock(&vol_mutex);
volume = pow(10.0,0.05*f); volume = pow(10.0,0.05*f);
fix_volume = 65536.0 * volume; fix_volume = 65536.0 * volume;
pthread_mutex_unlock(&vol_mutex);
continue; continue;
} }
if (!strcmp(line, "exit\n")) { if (!strcmp(line, "exit\n")) {
Expand Down Expand Up @@ -702,6 +705,7 @@ static int stuff_buffer(double playback_rate, short *inptr, short *outptr) {
stuffsamp = rand() % (frame_size - 1); stuffsamp = rand() % (frame_size - 1);
} }


pthread_mutex_lock(&vol_mutex);
for (i=0; i<stuffsamp; i++) { // the whole frame, if no stuffing for (i=0; i<stuffsamp; i++) { // the whole frame, if no stuffing
*outptr++ = dithered_vol(*inptr++); *outptr++ = dithered_vol(*inptr++);
*outptr++ = dithered_vol(*inptr++); *outptr++ = dithered_vol(*inptr++);
Expand All @@ -724,6 +728,7 @@ static int stuff_buffer(double playback_rate, short *inptr, short *outptr) {
*outptr++ = dithered_vol(*inptr++); *outptr++ = dithered_vol(*inptr++);
} }
} }
pthread_mutex_unlock(&vol_mutex);


return frame_size + stuff; return frame_size + stuff;
} }
Expand Down Expand Up @@ -759,11 +764,13 @@ static void *audio_thread_func(void *arg) {


#ifdef FANCY_RESAMPLING #ifdef FANCY_RESAMPLING
if (fancy_resampling) { if (fancy_resampling) {
int i; int i;
pthread_mutex_lock(&vol_mutex);
for (i=0; i<2*FRAME_BYTES; i++) { for (i=0; i<2*FRAME_BYTES; i++) {
frame[i] = (float)inbuf[i] / 32768.0; frame[i] = (float)inbuf[i] / 32768.0;
frame[i] *= volume; frame[i] *= volume;
} }
pthread_mutex_unlock(&vol_mutex);
srcdat.src_ratio = bf_playback_rate; srcdat.src_ratio = bf_playback_rate;
src_process(src, &srcdat); src_process(src, &srcdat);
assert(srcdat.input_frames_used == FRAME_BYTES); assert(srcdat.input_frames_used == FRAME_BYTES);
Expand Down

0 comments on commit 8b1728d

Please sign in to comment.