diff options
author | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-07-23 14:53:04 +0000 |
---|---|---|
committer | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-07-23 14:53:04 +0000 |
commit | 30502a0566de56d6f232372586f20835f51f405f (patch) | |
tree | 629fb617f7761f5dc7dab8df206b94f006db60f0 | |
parent | 8e16e1f335f019391e9a6cdbaf9f77f0f55e1b1d (diff) |
Avoid the use of memcpy() to make the Vorbis block stitching make sense
Rename 'struct sound' to 'struct sound_struct' to avoid potential confusion
Clip negative samples as well
Change the name of the music track to music.ogg
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@155 84d2e878-0bd5-11dd-ad15-13eda11d74c5
-rw-r--r-- | src/audio.c | 30 | ||||
-rw-r--r-- | src/types.h | 9 |
2 files changed, 24 insertions, 15 deletions
diff --git a/src/audio.c b/src/audio.c index 45efa47..f21bc9f 100644 --- a/src/audio.c +++ b/src/audio.c @@ -59,6 +59,11 @@ static void audio_mix(void *data, Uint8 *stream8, int len) { clip_count++; } stream[j] = 32767; + } else if ( test < -32767 ) { + if ( stream[j] != -32767 ) { + clip_count++; + } + stream[j] = -32767; } else { stream[j] += samp * a->sounds[i].volume; } @@ -75,9 +80,6 @@ static void audio_mix(void *data, Uint8 *stream8, int len) { break; } } - //if ( (i==0) && !(ppp++ % 10000) ) { - // printf("%li / %li (%.2f%%)\n", a->sounds[i].dpos, a->sounds[i].dlen, (double)a->sounds[i].dpos*100.0/a->sounds[i].dlen); - //} } @@ -219,13 +221,15 @@ static void *audio_play_vorbis(void *add_void) { goto out; } decode_done = 0; - decode_samples_done = 0; + decode_samples_done = 0; /* Number of samples from the Vorbis file (i.e. BEFORE CONVERSION) */ a->sounds[idx].decode_pos = 0; /* Position (in sounds[idx].data) at which to write the next block */ while ( !decode_done ) { - long decode_block_samples; + long decode_block_samples; /* Number of samples BEFORE CONVERSION */ long decode_block_length; int w; + int i; + Sint16 *cvtbuf16; /* Decide how much data to shovel this time */ decode_block_samples = 10*vi->rate; /* 10 seconds in samples */ @@ -259,8 +263,11 @@ static void *audio_play_vorbis(void *add_void) { if ( !a->sounds[idx].playing ) { a->sounds[idx].data = malloc(len*vi->channels*2); } - memcpy(a->sounds[idx].data+a->sounds[idx].decode_pos, cvt.buf, cvt.len_cvt); - a->sounds[idx].decode_pos += cvt.len_cvt / 2; + cvtbuf16 = (Sint16 *)cvt.buf; + for ( i=0; i<decode_block_samples*2; i++ ) { + a->sounds[idx].data[i+a->sounds[idx].decode_pos] = cvtbuf16[i]; + } + a->sounds[idx].decode_pos += decode_block_samples*2; /* It's safe to start playing at this point */ if ( !a->sounds[idx].playing ) { @@ -270,16 +277,17 @@ static void *audio_play_vorbis(void *add_void) { a->sounds[idx].volume = add->volume; a->sounds[idx].playing = 1; /* Must be done last - tell the mixer thread it can use this */ } - - if ( a->debug ) printf("AU: Channel %i: Decoded %li samples this time. " + if ( a->debug ) printf("AU: Channel %i: decode_pos=%i, dpos=%li. " "I am now %.1f seconds ahead of the playing position.\n", - idx, decode_block_samples, ((double)a->sounds[idx].decode_pos-(2*a->sounds[idx].dpos))/(44100*2*2)); + idx, a->sounds[idx].decode_pos, a->sounds[idx].dpos, + ((double)a->sounds[idx].decode_pos-a->sounds[idx].dpos)/(44100*2)); /* Sleep for eight seconds while periodically checking for shutdown */ for ( w=0; w<80; w++ ) { if ( a->shutdown ) { ov_clear(&vf); free(data); + printf("AU: Channel %i: Dispatch thread aborting.\n", idx); goto out; } usleep(100000); /* 0.1 seconds */ @@ -451,7 +459,7 @@ AudioContext *audio_setup(int debug, int no_music) { } if ( !no_music ) { - audio_play(a, "kraftwerk", 0.5, 1); + audio_play(a, "music", 0.5, 1); } else { if ( a->debug ) printf("AU: Music disabled.\n"); } diff --git a/src/types.h b/src/types.h index 950d26e..fb59f2d 100644 --- a/src/types.h +++ b/src/types.h @@ -158,15 +158,16 @@ typedef struct { #define AUDIO_MAX_SOUNDS 16 typedef struct { - struct sound { + /* There are two mono samples in a stereo sample. */ + struct sound_struct { Sint16 *data; - long dpos; - long dlen; + long dpos; /* Number of (mono) samples played so far */ + long dlen; /* Number of (mono) samples in 'data' */ int inuse; int playing; int repeat; float volume; - size_t decode_pos; + size_t decode_pos; /* Number of (mono) samples decoded so far */ } sounds[AUDIO_MAX_SOUNDS]; pthread_mutex_t sounds_mutex; /* Mutex for the list of sounds being played */ |