diff options
Diffstat (limited to 'apps/pcmbuf.c')
-rw-r--r-- | apps/pcmbuf.c | 1005 |
1 files changed, 513 insertions, 492 deletions
diff --git a/apps/pcmbuf.c b/apps/pcmbuf.c index 1e286fafc6..ad1dfca34d 100644 --- a/apps/pcmbuf.c +++ b/apps/pcmbuf.c @@ -142,13 +142,13 @@ extern unsigned int codec_thread_id; #define LOW_DATA(quarter_secs) \ (pcmbuf_unplayed_bytes < NATIVE_FREQUENCY * quarter_secs) -static bool prepare_insert(size_t length); -static void pcmbuf_under_watermark(bool under); static bool pcmbuf_flush_fillpos(void); - +static void pcmbuf_finish_track_change(void); +static void crossfade_start(void); +static void flush_crossfade(char *buf, size_t length); static bool pcmbuf_crossfade_init(bool manual_skip); -static bool pcmbuf_is_crossfade_enabled(void); static void pcmbuf_finish_crossfade_enable(void); +static bool pcmbuf_is_crossfade_enabled(void); /**************************************/ @@ -190,141 +190,157 @@ static bool show_desc(char *caller) #define DISPLAY_DESC(caller) do{}while(0) #endif -/* Track change functions */ -/* The codec is moving on to the next track, but the current track is - * still playing. Set flags to make sure the elapsed time of the current - * track is updated properly, and mark the currently written chunk as the - * last one in the track. */ -static void pcmbuf_gapless_track_change(void) -{ - /* we're starting a track transition */ - track_transition = true; - - /* mark the last chunk in the track */ - end_of_track = true; -} +/* Commit PCM data */ -static void pcmbuf_crossfade_track_change(void) +#ifdef HAVE_PRIORITY_SCHEDULING +static void boost_codec_thread(bool boost) { - /* Initiate automatic crossfade mode */ - pcmbuf_crossfade_init(false); - /* Notify the wps that the track change starts now */ - audio_post_track_change(false); -} + /* Keep voice and codec threads at the same priority or else voice + * will starve if the codec thread's priority is boosted. */ + if (boost) + { + int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes + / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX; -void pcmbuf_start_track_change(bool manual_skip) -{ - /* Manual track change (always crossfade or flush audio). */ - if (manual_skip) + if (priority != codec_thread_priority) + { + codec_thread_priority = priority; + thread_set_priority(codec_thread_id, priority); + voice_thread_set_priority(priority); + } + } + else if (codec_thread_priority != PRIORITY_PLAYBACK) { - pcmbuf_crossfade_init(true); - audio_post_track_change(false); + thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK); + voice_thread_set_priority(PRIORITY_PLAYBACK); + codec_thread_priority = PRIORITY_PLAYBACK; } - /* Automatic track change w/crossfade, if not in "Track Skip Only" mode. */ - else if (pcmbuf_is_crossfade_enabled() && !pcmbuf_is_crossfade_active() - && global_settings.crossfade != CROSSFADE_ENABLE_TRACKSKIP) +} +#endif /* HAVE_PRIORITY_SCHEDULING */ + +static void pcmbuf_under_watermark(bool under) +{ + /* Only codec thread initiates boost - voice boosts the cpu when playing + a clip */ +#ifndef SIMULATOR + if (thread_get_current() == codec_thread_id) +#endif /* SIMULATOR */ { - if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE_AND_TRACKSKIP) + if (under) { - if (global_settings.playlist_shuffle) - /* shuffle mode is on, so crossfade: */ - pcmbuf_crossfade_track_change(); - else - /* shuffle mode is off, so normal gapless playback */ - pcmbuf_gapless_track_change(); + /* Fill audio buffer by boosting cpu */ + trigger_cpu_boost(); +#ifdef HAVE_PRIORITY_SCHEDULING + /* If buffer is critically low, override UI priority, else + set back to the original priority. */ + boost_codec_thread(LOW_DATA(2) && pcm_is_playing()); +#endif } else - /* normal crossfade: */ - pcmbuf_crossfade_track_change(); + { +#ifdef HAVE_PRIORITY_SCHEDULING + boost_codec_thread(false); +#endif + } } - else - /* normal gapless playback. */ - pcmbuf_gapless_track_change(); -} -/* Called when the last chunk in the track has been played */ -static void pcmbuf_finish_track_change(void) -{ - /* not in a track transition anymore */ - if(track_transition){logf("pcmbuf: (finish change) track transition false");} - track_transition = false; - - /* notify playback that the track has just finished */ - audio_post_track_change(true); + /* Disable crossfade if < .5s of audio */ + if (LOW_DATA(2)) + { + crossfade_active = false; + } } - -/** PCM driver callback - * This function has 3 major logical parts (separated by brackets both for - * readability and variable scoping). The first part performs the - * operations related to finishing off the last buffer we fed to the DMA. - * The second part detects the end of playlist condition when the pcm - * buffer is empty except for uncommitted samples. Then they are committed. - * The third part performs the operations involved in sending a new buffer - * to the DMA. */ -static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) ICODE_ATTR; -static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) +static bool prepare_insert(size_t length) { + if (low_latency_mode) { - struct pcmbufdesc *pcmbuf_current = pcmbuf_read; - /* Take the finished buffer out of circulation */ - pcmbuf_read = pcmbuf_current->link; - - /* if during a track transition, update the elapsed time */ - if (track_transition) - audio_pcmbuf_position_callback(last_chunksize); - - /* if last buffer in the track, let the audio thread know */ - if (pcmbuf_current->end_of_track) - pcmbuf_finish_track_change(); + /* 1/4s latency. */ + if (!LOW_DATA(1) && pcm_is_playing()) + return false; + } - /* Put the finished buffer back into circulation */ - pcmbuf_write_end->link = pcmbuf_current; - pcmbuf_write_end = pcmbuf_current; + /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */ + if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK) + return false; - /* If we've read over the mix chunk while it's still mixing there */ - if (pcmbuf_current == pcmbuf_mix_chunk) - pcmbuf_mix_chunk = NULL; - /* If we've read over the crossfade chunk while it's still fading */ - if (pcmbuf_current == crossfade_chunk) - crossfade_chunk = pcmbuf_read; - } - + if (!pcm_is_playing()) { - /* Commit last samples at end of playlist */ - if (audiobuffer_fillpos && !pcmbuf_read) + trigger_cpu_boost(); + + /* Pre-buffer up to watermark */ +#if MEMORYSIZE > 2 + if (!LOW_DATA(4)) +#else + if (pcmbuf_unplayed_bytes > pcmbuf_watermark) +#endif { - logf("pcmbuf_pcm_callback: commit last samples"); - pcmbuf_flush_fillpos(); + logf("pcm starting"); + if (!(audio_status() & AUDIO_STATUS_PAUSE)) + pcmbuf_play_start(); } } + else + pcmbuf_under_watermark(pcmbuf_unplayed_bytes <= pcmbuf_watermark); + + return true; +} + +void *pcmbuf_request_buffer(int *count) +{ +#ifdef HAVE_CROSSFADE + if (crossfade_init) + crossfade_start(); +#endif + if (crossfade_active) { + *count = MIN(*count, PCMBUF_MIX_CHUNK/4); + return fadebuf; + } + else { - /* Send the new buffer to the pcm */ - struct pcmbufdesc *pcmbuf_new = pcmbuf_read; - size_t *realsize = size; - unsigned char** realstart = start; - if(pcmbuf_new) + if(prepare_insert(*count << 2)) { - size_t current_size = pcmbuf_new->size; - - pcmbuf_unplayed_bytes -= current_size; - last_chunksize = current_size; - *realsize = current_size; - *realstart = pcmbuf_new->addr; + size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos; + if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK) + { + /* Usual case, there's space here */ + return &audiobuffer[audiobuffer_index]; + } + else + { + /* Flush and wrap the buffer */ + pcmbuf_flush_fillpos(); + audiobuffer_pos = 0; + return &audiobuffer[0]; + } } else { - /* No more buffers */ - last_chunksize = 0; - *realsize = 0; - *realstart = NULL; - if (end_of_track) - pcmbuf_finish_track_change(); + return NULL; } } - DISPLAY_DESC("callback"); +} + +void pcmbuf_write_complete(int count) +{ + size_t length = (size_t)(unsigned int)count << 2; +#ifdef HAVE_CROSSFADE + if (crossfade_active) + { + flush_crossfade(fadebuf, length); + if (!(crossfade_fade_in_rem || crossfade_chunk)) + crossfade_active = false; + } + else +#endif + { + audiobuffer_fillpos += length; + + if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos)) + pcmbuf_flush_fillpos(); + } } /* This is really just part of pcmbuf_flush_fillpos, but is easier to keep @@ -396,177 +412,8 @@ static bool pcmbuf_flush_fillpos(void) return false; } -#ifdef HAVE_PRIORITY_SCHEDULING -static void boost_codec_thread(bool boost) -{ - /* Keep voice and codec threads at the same priority or else voice - * will starve if the codec thread's priority is boosted. */ - if (boost) - { - int priority = (PRIORITY_PLAYBACK - PRIORITY_PLAYBACK_MAX)*pcmbuf_unplayed_bytes - / (2*NATIVE_FREQUENCY) + PRIORITY_PLAYBACK_MAX; - if (priority != codec_thread_priority) - { - codec_thread_priority = priority; - thread_set_priority(codec_thread_id, priority); - voice_thread_set_priority(priority); - } - } - else if (codec_thread_priority != PRIORITY_PLAYBACK) - { - thread_set_priority(codec_thread_id, PRIORITY_PLAYBACK); - voice_thread_set_priority(PRIORITY_PLAYBACK); - codec_thread_priority = PRIORITY_PLAYBACK; - } -} -#endif /* HAVE_PRIORITY_SCHEDULING */ - -static void pcmbuf_under_watermark(bool under) -{ - /* Only codec thread initiates boost - voice boosts the cpu when playing - a clip */ -#ifndef SIMULATOR - if (thread_get_current() == codec_thread_id) -#endif /* SIMULATOR */ - { - if (under) - { - /* Fill audio buffer by boosting cpu */ - trigger_cpu_boost(); -#ifdef HAVE_PRIORITY_SCHEDULING - /* If buffer is critically low, override UI priority, else - set back to the original priority. */ - boost_codec_thread(LOW_DATA(2) && pcm_is_playing()); -#endif - } - else - { -#ifdef HAVE_PRIORITY_SCHEDULING - boost_codec_thread(false); -#endif - } - } - - /* Disable crossfade if < .5s of audio */ - if (LOW_DATA(2)) - { - crossfade_active = false; - } -} - -unsigned long pcmbuf_get_latency(void) -{ - /* Be careful how this calculation is rearranged, it's easy to overflow */ - size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting(); - return bytes / 4 * 1000 / NATIVE_FREQUENCY; -} - -void pcmbuf_set_low_latency(bool state) -{ - low_latency_mode = state; -} - -bool pcmbuf_is_lowdata(void) -{ - if (!pcm_is_playing() || pcm_is_paused() || - crossfade_init || crossfade_active) - return false; - -#if MEMORYSIZE > 2 - /* 1 seconds of buffer is low data */ - return LOW_DATA(4); -#else - /* under watermark is low data */ - return (pcmbuf_unplayed_bytes < pcmbuf_watermark); -#endif -} - -/* Amount of bytes left in the buffer. */ -inline size_t pcmbuf_free(void) -{ - if (pcmbuf_read != NULL) - { - void *read = pcmbuf_read->addr; - void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos]; - if (read < write) - return (size_t)(read - write) + pcmbuf_size; - else - return (size_t) (read - write); - } - return pcmbuf_size; -} - -static bool pcmbuf_crossfade_init(bool manual_skip) -{ - /* Can't do two crossfades at once and, no fade if pcm is off now */ - if (crossfade_init || crossfade_active || !pcm_is_playing()) - { - pcmbuf_play_stop(); - return false; - } - - trigger_cpu_boost(); - - /* Not enough data, or crossfade disabled, flush the old data instead */ - if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode) - { - pcmbuf_flush_fillpos(); - pcmbuf_flush = true; - return false; - } - - /* Don't enable mix mode when skipping tracks manually. */ - if (manual_skip) - crossfade_mixmode = false; - else - crossfade_mixmode = global_settings.crossfade_fade_out_mixmode; - - crossfade_init = true; - - return true; - -} - -void pcmbuf_play_stop(void) -{ - pcm_play_stop(); - - pcmbuf_unplayed_bytes = 0; - pcmbuf_mix_chunk = NULL; - if (pcmbuf_read) { - pcmbuf_write_end->link = pcmbuf_read; - pcmbuf_write_end = pcmbuf_read_end; - pcmbuf_read = pcmbuf_read_end = NULL; - } - audiobuffer_pos = 0; - audiobuffer_fillpos = 0; - crossfade_init = false; - crossfade_active = false; - pcmbuf_flush = false; - DISPLAY_DESC("play_stop"); - -#ifdef HAVE_PRIORITY_SCHEDULING - /* Can unboost the codec thread here no matter who's calling */ - boost_codec_thread(false); -#endif -} - -int pcmbuf_used_descs(void) -{ - struct pcmbufdesc *pcmbuf_temp = pcmbuf_read; - unsigned int i = 0; - while (pcmbuf_temp) { - pcmbuf_temp = pcmbuf_temp->link; - i++; - } - return i; -} - -int pcmbuf_descs(void) -{ - return PCMBUF_DESCS(pcmbuf_size); -} +/* Init */ static void pcmbuf_init_pcmbuffers(void) { @@ -606,24 +453,6 @@ static char *pcmbuf_calc_audiobuffer_ptr(size_t bufsize) PCMBUF_DESCS_SIZE(bufsize)); } -bool pcmbuf_is_same_size(void) -{ - bool same_size; - - if (audiobuffer == NULL) - same_size = true; /* Not set up yet even once so always */ - else - { - size_t bufsize = pcmbuf_get_next_required_pcmbuf_size(); - same_size = pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer; - } - - if (same_size) - pcmbuf_finish_crossfade_enable(); - - return same_size; -} - /* Initialize the pcmbuffer the structure looks like this: * ...|---------PCMBUF---------|FADEBUF|VOICEBUF|DESCS|... */ size_t pcmbuf_init(unsigned char *bufend) @@ -648,18 +477,116 @@ size_t pcmbuf_init(unsigned char *bufend) return pcmbuf_bufend - audiobuffer; } -size_t pcmbuf_get_bufsize(void) + +/* Playback */ + +/** PCM driver callback + * This function has 3 major logical parts (separated by brackets both for + * readability and variable scoping). The first part performs the + * operations related to finishing off the last buffer we fed to the DMA. + * The second part detects the end of playlist condition when the pcm + * buffer is empty except for uncommitted samples. Then they are committed. + * The third part performs the operations involved in sending a new buffer + * to the DMA. */ +static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) ICODE_ATTR; +static void pcmbuf_pcm_callback(unsigned char** start, size_t* size) { - return pcmbuf_size; + { + struct pcmbufdesc *pcmbuf_current = pcmbuf_read; + /* Take the finished buffer out of circulation */ + pcmbuf_read = pcmbuf_current->link; + + /* if during a track transition, update the elapsed time */ + if (track_transition) + audio_pcmbuf_position_callback(last_chunksize); + + /* if last buffer in the track, let the audio thread know */ + if (pcmbuf_current->end_of_track) + pcmbuf_finish_track_change(); + + /* Put the finished buffer back into circulation */ + pcmbuf_write_end->link = pcmbuf_current; + pcmbuf_write_end = pcmbuf_current; + + /* If we've read over the mix chunk while it's still mixing there */ + if (pcmbuf_current == pcmbuf_mix_chunk) + pcmbuf_mix_chunk = NULL; + /* If we've read over the crossfade chunk while it's still fading */ + if (pcmbuf_current == crossfade_chunk) + crossfade_chunk = pcmbuf_read; + } + + { + /* Commit last samples at end of playlist */ + if (audiobuffer_fillpos && !pcmbuf_read) + { + logf("pcmbuf_pcm_callback: commit last samples"); + pcmbuf_flush_fillpos(); + } + } + + { + /* Send the new buffer to the pcm */ + struct pcmbufdesc *pcmbuf_new = pcmbuf_read; + size_t *realsize = size; + unsigned char** realstart = start; + if(pcmbuf_new) + { + size_t current_size = pcmbuf_new->size; + + pcmbuf_unplayed_bytes -= current_size; + last_chunksize = current_size; + *realsize = current_size; + *realstart = pcmbuf_new->addr; + } + else + { + /* No more buffers */ + last_chunksize = 0; + *realsize = 0; + *realstart = NULL; + if (end_of_track) + pcmbuf_finish_track_change(); + } + } + DISPLAY_DESC("callback"); } -#ifdef ROCKBOX_HAS_LOGF -unsigned char * pcmbuf_get_meminfo(size_t *length) +/* Force playback. */ +void pcmbuf_play_start(void) { - *length = pcmbuf_bufend - audiobuffer; - return audiobuffer; + if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL) + { + last_chunksize = pcmbuf_read->size; + pcmbuf_unplayed_bytes -= last_chunksize; + pcm_play_data(pcmbuf_pcm_callback, + (unsigned char *)pcmbuf_read->addr, last_chunksize); + } } + +void pcmbuf_play_stop(void) +{ + pcm_play_stop(); + + pcmbuf_unplayed_bytes = 0; + pcmbuf_mix_chunk = NULL; + if (pcmbuf_read) { + pcmbuf_write_end->link = pcmbuf_read; + pcmbuf_write_end = pcmbuf_read_end; + pcmbuf_read = pcmbuf_read_end = NULL; + } + audiobuffer_pos = 0; + audiobuffer_fillpos = 0; + crossfade_init = false; + crossfade_active = false; + pcmbuf_flush = false; + DISPLAY_DESC("play_stop"); + +#ifdef HAVE_PRIORITY_SCHEDULING + /* Can unboost the codec thread here no matter who's calling */ + boost_codec_thread(false); #endif +} void pcmbuf_pause(bool pause) { @@ -669,18 +596,74 @@ void pcmbuf_pause(bool pause) pcmbuf_play_start(); } -/* Force playback. */ -void pcmbuf_play_start(void) + +/* Track change */ + +/* The codec is moving on to the next track, but the current track is + * still playing. Set flags to make sure the elapsed time of the current + * track is updated properly, and mark the currently written chunk as the + * last one in the track. */ +static void pcmbuf_gapless_track_change(void) { - if (!pcm_is_playing() && pcmbuf_unplayed_bytes && pcmbuf_read != NULL) + /* we're starting a track transition */ + track_transition = true; + + /* mark the last chunk in the track */ + end_of_track = true; +} + +static void pcmbuf_crossfade_track_change(void) +{ + /* Initiate automatic crossfade mode */ + pcmbuf_crossfade_init(false); + /* Notify the wps that the track change starts now */ + audio_post_track_change(false); +} + +void pcmbuf_start_track_change(bool manual_skip) +{ + /* Manual track change (always crossfade or flush audio). */ + if (manual_skip) { - last_chunksize = pcmbuf_read->size; - pcmbuf_unplayed_bytes -= last_chunksize; - pcm_play_data(pcmbuf_pcm_callback, - (unsigned char *)pcmbuf_read->addr, last_chunksize); + pcmbuf_crossfade_init(true); + audio_post_track_change(false); } + /* Automatic track change w/crossfade, if not in "Track Skip Only" mode. */ + else if (pcmbuf_is_crossfade_enabled() && !pcmbuf_is_crossfade_active() + && global_settings.crossfade != CROSSFADE_ENABLE_TRACKSKIP) + { + if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE_AND_TRACKSKIP) + { + if (global_settings.playlist_shuffle) + /* shuffle mode is on, so crossfade: */ + pcmbuf_crossfade_track_change(); + else + /* shuffle mode is off, so normal gapless playback */ + pcmbuf_gapless_track_change(); + } + else + /* normal crossfade: */ + pcmbuf_crossfade_track_change(); + } + else + /* normal gapless playback. */ + pcmbuf_gapless_track_change(); +} + +/* Called when the last chunk in the track has been played */ +static void pcmbuf_finish_track_change(void) +{ + /* not in a track transition anymore */ + if(track_transition){logf("pcmbuf: (finish change) track transition false");} + track_transition = false; + + /* notify playback that the track has just finished */ + audio_post_track_change(true); } + +/* Crossfade */ + /** * Low memory targets don't have crossfade, so don't compile crossfade * specific code in order to save some memory. */ @@ -925,95 +908,257 @@ static void flush_crossfade(char *buf, size_t length) } #endif /* HAVE_CROSSFADE */ -static bool prepare_insert(size_t length) +static bool pcmbuf_crossfade_init(bool manual_skip) { - if (low_latency_mode) + /* Can't do two crossfades at once and, no fade if pcm is off now */ + if (crossfade_init || crossfade_active || !pcm_is_playing()) { - /* 1/4s latency. */ - if (!LOW_DATA(1) && pcm_is_playing()) - return false; + pcmbuf_play_stop(); + return false; } - /* Need to save PCMBUF_MIN_CHUNK to prevent wrapping overwriting */ - if (pcmbuf_free() < length + PCMBUF_MIN_CHUNK) - return false; + trigger_cpu_boost(); - if (!pcm_is_playing()) + /* Not enough data, or crossfade disabled, flush the old data instead */ + if (LOW_DATA(2) || !pcmbuf_is_crossfade_enabled() || low_latency_mode) { - trigger_cpu_boost(); - - /* Pre-buffer up to watermark */ -#if MEMORYSIZE > 2 - if (!LOW_DATA(4)) -#else - if (pcmbuf_unplayed_bytes > pcmbuf_watermark) -#endif - { - logf("pcm starting"); - if (!(audio_status() & AUDIO_STATUS_PAUSE)) - pcmbuf_play_start(); - } + pcmbuf_flush_fillpos(); + pcmbuf_flush = true; + return false; } + + /* Don't enable mix mode when skipping tracks manually. */ + if (manual_skip) + crossfade_mixmode = false; else - pcmbuf_under_watermark(pcmbuf_unplayed_bytes <= pcmbuf_watermark); + crossfade_mixmode = global_settings.crossfade_fade_out_mixmode; + + crossfade_init = true; return true; + } -void *pcmbuf_request_buffer(int *count) +static void pcmbuf_finish_crossfade_enable(void) { -#ifdef HAVE_CROSSFADE - if (crossfade_init) - crossfade_start(); -#endif + /* Copy the pending setting over now */ + crossfade_enabled = crossfade_enabled_pending; + + pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ? + /* If crossfading, try to keep the buffer full other than 1 second */ + (pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) : + /* Otherwise, just use the default */ + PCMBUF_WATERMARK; +} - if (crossfade_active) { - *count = MIN(*count, PCMBUF_MIX_CHUNK/4); - return fadebuf; - } +static bool pcmbuf_is_crossfade_enabled(void) +{ + if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE) + return global_settings.playlist_shuffle; + + return crossfade_enabled; +} + +bool pcmbuf_is_crossfade_active(void) +{ + return crossfade_active || crossfade_init; +} + +void pcmbuf_request_crossfade_enable(bool on_off) +{ + /* Next setting to be used, not applied now */ + crossfade_enabled_pending = on_off; +} + +bool pcmbuf_is_same_size(void) +{ + bool same_size; + + if (audiobuffer == NULL) + same_size = true; /* Not set up yet even once so always */ else { - if(prepare_insert(*count << 2)) + size_t bufsize = pcmbuf_get_next_required_pcmbuf_size(); + same_size = pcmbuf_calc_audiobuffer_ptr(bufsize) == audiobuffer; + } + + if (same_size) + pcmbuf_finish_crossfade_enable(); + + return same_size; +} + + +/* Voice */ + +/* Returns pcm buffer usage in percents (0 to 100). */ +static int pcmbuf_usage(void) +{ + return pcmbuf_unplayed_bytes * 100 / pcmbuf_size; +} + +static int pcmbuf_mix_free(void) +{ + if (pcmbuf_mix_chunk) + { + size_t my_mix_end = + (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample]; + size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos]; + if (my_write_pos < my_mix_end) + my_write_pos += pcmbuf_size; + return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes; + } + return 100; +} + +void *pcmbuf_request_voice_buffer(int *count) +{ + /* A get-it-to-work-for-now hack (audio status could change by + completion) */ + if (audio_status() & AUDIO_STATUS_PLAY) + { + if (pcmbuf_read == NULL) { - size_t audiobuffer_index = audiobuffer_pos + audiobuffer_fillpos; - if (pcmbuf_size - audiobuffer_index >= PCMBUF_MIN_CHUNK) - { - /* Usual case, there's space here */ - return &audiobuffer[audiobuffer_index]; - } - else - { - /* Flush and wrap the buffer */ - pcmbuf_flush_fillpos(); - audiobuffer_pos = 0; - return &audiobuffer[0]; - } + return NULL; + } + else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 && + (pcmbuf_mix_chunk || pcmbuf_read->link)) + { + *count = MIN(*count, PCMBUF_MIX_CHUNK/4); + return voicebuf; } else { return NULL; } } + else + { + return pcmbuf_request_buffer(count); + } } -void pcmbuf_write_complete(int count) +void pcmbuf_write_voice_complete(int count) { - size_t length = (size_t)(unsigned int)count << 2; -#ifdef HAVE_CROSSFADE - if (crossfade_active) + /* A get-it-to-work-for-now hack (audio status could have changed) */ + if (!(audio_status() & AUDIO_STATUS_PLAY)) { - flush_crossfade(fadebuf, length); - if (!(crossfade_fade_in_rem || crossfade_chunk)) - crossfade_active = false; + pcmbuf_write_complete(count); + return; } - else -#endif + + int16_t *ibuf = (int16_t *)voicebuf; + int16_t *obuf; + size_t chunk_samples; + + if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL) { - audiobuffer_fillpos += length; + pcmbuf_mix_chunk = pcmbuf_read->link; + /* Start 1/8s into the next chunk */ + pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16; + } - if (NEED_FLUSH(audiobuffer_pos + audiobuffer_fillpos)) - pcmbuf_flush_fillpos(); + if (!pcmbuf_mix_chunk) + return; + + obuf = (int16_t *)pcmbuf_mix_chunk->addr; + chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t); + + count <<= 1; + + while (count-- > 0) + { + int32_t sample = *ibuf++; + + if (pcmbuf_mix_sample >= chunk_samples) + { + pcmbuf_mix_chunk = pcmbuf_mix_chunk->link; + if (!pcmbuf_mix_chunk) + return; + pcmbuf_mix_sample = 0; + obuf = pcmbuf_mix_chunk->addr; + chunk_samples = pcmbuf_mix_chunk->size / 2; + } + sample += obuf[pcmbuf_mix_sample] >> 2; + obuf[pcmbuf_mix_sample++] = clip_sample_16(sample); + } +} + + +/* Debug menu, other metrics */ + +/* Amount of bytes left in the buffer. */ +size_t pcmbuf_free(void) +{ + if (pcmbuf_read != NULL) + { + void *read = pcmbuf_read->addr; + void *write = &audiobuffer[audiobuffer_pos + audiobuffer_fillpos]; + if (read < write) + return (size_t)(read - write) + pcmbuf_size; + else + return (size_t) (read - write); } + return pcmbuf_size; +} + +size_t pcmbuf_get_bufsize(void) +{ + return pcmbuf_size; +} + +int pcmbuf_used_descs(void) +{ + struct pcmbufdesc *pcmbuf_temp = pcmbuf_read; + unsigned int i = 0; + while (pcmbuf_temp) { + pcmbuf_temp = pcmbuf_temp->link; + i++; + } + return i; +} + +int pcmbuf_descs(void) +{ + return PCMBUF_DESCS(pcmbuf_size); +} + +#ifdef ROCKBOX_HAS_LOGF +unsigned char * pcmbuf_get_meminfo(size_t *length) +{ + *length = pcmbuf_bufend - audiobuffer; + return audiobuffer; +} +#endif + + +/* Misc */ + +bool pcmbuf_is_lowdata(void) +{ + if (!pcm_is_playing() || pcm_is_paused() || + crossfade_init || crossfade_active) + return false; + +#if MEMORYSIZE > 2 + /* 1 seconds of buffer is low data */ + return LOW_DATA(4); +#else + /* under watermark is low data */ + return (pcmbuf_unplayed_bytes < pcmbuf_watermark); +#endif +} + +void pcmbuf_set_low_latency(bool state) +{ + low_latency_mode = state; +} + +unsigned long pcmbuf_get_latency(void) +{ + /* Be careful how this calculation is rearranged, it's easy to overflow */ + size_t bytes = pcmbuf_unplayed_bytes + pcm_get_bytes_waiting(); + return bytes / 4 * 1000 / NATIVE_FREQUENCY; } #ifndef HAVE_HARDWARE_BEEP @@ -1117,127 +1262,3 @@ void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude) #endif } #endif /* HAVE_HARDWARE_BEEP */ - -/* Returns pcm buffer usage in percents (0 to 100). */ -static int pcmbuf_usage(void) -{ - return pcmbuf_unplayed_bytes * 100 / pcmbuf_size; -} - -static int pcmbuf_mix_free(void) -{ - if (pcmbuf_mix_chunk) - { - size_t my_mix_end = - (size_t)&((int16_t *)pcmbuf_mix_chunk->addr)[pcmbuf_mix_sample]; - size_t my_write_pos = (size_t)&audiobuffer[audiobuffer_pos]; - if (my_write_pos < my_mix_end) - my_write_pos += pcmbuf_size; - return (my_write_pos - my_mix_end) * 100 / pcmbuf_unplayed_bytes; - } - return 100; -} - -void *pcmbuf_request_voice_buffer(int *count) -{ - /* A get-it-to-work-for-now hack (audio status could change by - completion) */ - if (audio_status() & AUDIO_STATUS_PLAY) - { - if (pcmbuf_read == NULL) - { - return NULL; - } - else if (pcmbuf_usage() >= 10 && pcmbuf_mix_free() >= 30 && - (pcmbuf_mix_chunk || pcmbuf_read->link)) - { - *count = MIN(*count, PCMBUF_MIX_CHUNK/4); - return voicebuf; - } - else - { - return NULL; - } - } - else - { - return pcmbuf_request_buffer(count); - } -} - -void pcmbuf_write_voice_complete(int count) -{ - /* A get-it-to-work-for-now hack (audio status could have changed) */ - if (!(audio_status() & AUDIO_STATUS_PLAY)) - { - pcmbuf_write_complete(count); - return; - } - - int16_t *ibuf = (int16_t *)voicebuf; - int16_t *obuf; - size_t chunk_samples; - - if (pcmbuf_mix_chunk == NULL && pcmbuf_read != NULL) - { - pcmbuf_mix_chunk = pcmbuf_read->link; - /* Start 1/8s into the next chunk */ - pcmbuf_mix_sample = NATIVE_FREQUENCY * 4 / 16; - } - - if (!pcmbuf_mix_chunk) - return; - - obuf = (int16_t *)pcmbuf_mix_chunk->addr; - chunk_samples = pcmbuf_mix_chunk->size / sizeof (int16_t); - - count <<= 1; - - while (count-- > 0) - { - int32_t sample = *ibuf++; - - if (pcmbuf_mix_sample >= chunk_samples) - { - pcmbuf_mix_chunk = pcmbuf_mix_chunk->link; - if (!pcmbuf_mix_chunk) - return; - pcmbuf_mix_sample = 0; - obuf = pcmbuf_mix_chunk->addr; - chunk_samples = pcmbuf_mix_chunk->size / 2; - } - sample += obuf[pcmbuf_mix_sample] >> 2; - obuf[pcmbuf_mix_sample++] = clip_sample_16(sample); - } -} - -void pcmbuf_request_crossfade_enable(bool on_off) -{ - /* Next setting to be used, not applied now */ - crossfade_enabled_pending = on_off; -} - -static void pcmbuf_finish_crossfade_enable(void) -{ - /* Copy the pending setting over now */ - crossfade_enabled = crossfade_enabled_pending; - - pcmbuf_watermark = (crossfade_enabled && pcmbuf_size) ? - /* If crossfading, try to keep the buffer full other than 1 second */ - (pcmbuf_size - (NATIVE_FREQUENCY * 4 * 1)) : - /* Otherwise, just use the default */ - PCMBUF_WATERMARK; -} - -static bool pcmbuf_is_crossfade_enabled(void) -{ - if (global_settings.crossfade == CROSSFADE_ENABLE_SHUFFLE) - return global_settings.playlist_shuffle; - - return crossfade_enabled; -} - -bool pcmbuf_is_crossfade_active(void) -{ - return crossfade_active || crossfade_init; -} |