From c51fe40a7d7cf83dc2625920b7fbfe858f4cb14e Mon Sep 17 00:00:00 2001 From: Michael Sevakis Date: Thu, 26 Apr 2012 15:04:58 -0400 Subject: [PATCH] test_codec: Fix some problems with writing WAV with DSP On big-endian architecture, DSP output must be coverted to little- endian first. DSP output is also always interleaved stereo, 16 bit, NATIVE_FREQUENCY and wavinfo should be correct for this. Also, use standard clip_sample_16 already available. Change-Id: Ifa7b9fc77f0573070c7e79f059dc3000c437c42e --- apps/plugins/test_codec.c | 75 ++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/apps/plugins/test_codec.c b/apps/plugins/test_codec.c index b0f48fca4d..dafcf35710 100644 --- a/apps/plugins/test_codec.c +++ b/apps/plugins/test_codec.c @@ -245,14 +245,6 @@ static int process_dsp(const void *ch1, const void *ch2, int count) return written_count; } -static inline int32_t clip_sample(int32_t sample) -{ - if ((int16_t)sample != sample) - sample = 0x7fff ^ (sample >> 31); - - return sample; -} - /* Null output */ static void pcmbuf_insert_null(const void *ch1, const void *ch2, int count) { @@ -304,40 +296,39 @@ static int fill_buffer(int new_offset){ /* WAV output or calculate crc32 of output*/ static void pcmbuf_insert_wav_checksum(const void *ch1, const void *ch2, int count) { - const int16_t* data1_16; - const int16_t* data2_16; - const int32_t* data1_32; - const int32_t* data2_32; - unsigned char* p = wavbuffer; - const int scale = wavinfo.sampledepth - 15; - const int dc_bias = 1 << (scale - 1); - int channels = (wavinfo.stereomode == STEREO_MONO) ? 1 : 2; - /* Prevent idle poweroff */ rb->reset_poweroff_timer(); if (use_dsp) { count = process_dsp(ch1, ch2, count); wavinfo.totalsamples += count; - if (channels == 1) - { - unsigned char *s = dspbuffer, *d = dspbuffer; - int c = count; - while (c-- > 0) - { - *d++ = *s++; - *d++ = *s++; - s++; - s++; - } + +#ifdef ROCKBOX_BIG_ENDIAN + unsigned char* p = dspbuffer; + int i; + for (i = 0; i < count; i++) { + int2le16(p,*(int16_t *)p); + p += 2; + int2le16(p,*(int16_t *)p); + p += 2; + } +#endif + if (checksum) { + crc32 = rb->crc_32(dspbuffer, count * 2 * sizeof (int16_t), crc32); + } else { + rb->write(wavinfo.fd, dspbuffer, count * 2 * sizeof (int16_t)); } - if (checksum) - crc32 = rb->crc_32(dspbuffer, count * 2 * channels, crc32); - else - rb->write(wavinfo.fd, dspbuffer, count * 2 * channels); } else { + const int16_t* data1_16; + const int16_t* data2_16; + const int32_t* data1_32; + const int32_t* data2_32; + unsigned char* p = wavbuffer; + const int scale = wavinfo.sampledepth - 15; + const int dc_bias = 1 << (scale - 1); + if (wavinfo.sampledepth <= 16) { data1_16 = ch1; data2_16 = ch2; @@ -378,18 +369,18 @@ static void pcmbuf_insert_wav_checksum(const void *ch1, const void *ch2, int cou { case STEREO_INTERLEAVED: while (count--) { - int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale)); + int2le16(p, clip_sample_16((*data1_32++ + dc_bias) >> scale)); p += 2; - int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale)); + int2le16(p, clip_sample_16((*data1_32++ + dc_bias) >> scale)); p += 2; } break; case STEREO_NONINTERLEAVED: while (count--) { - int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale)); + int2le16(p, clip_sample_16((*data1_32++ + dc_bias) >> scale)); p += 2; - int2le16(p, clip_sample((*data2_32++ + dc_bias) >> scale)); + int2le16(p, clip_sample_16((*data2_32++ + dc_bias) >> scale)); p += 2; } @@ -397,7 +388,7 @@ static void pcmbuf_insert_wav_checksum(const void *ch1, const void *ch2, int cou case STEREO_MONO: while (count--) { - int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale)); + int2le16(p, clip_sample_16((*data1_32++ + dc_bias) >> scale)); p += 2; } break; @@ -514,17 +505,17 @@ static void configure(int setting, intptr_t value) case DSP_SWITCH_FREQUENCY: case DSP_SET_FREQUENCY: DEBUGF("samplerate=%d\n",(int)value); - wavinfo.samplerate = (int)value; + wavinfo.samplerate = use_dsp ? NATIVE_FREQUENCY : (int)value; break; case DSP_SET_SAMPLE_DEPTH: DEBUGF("sampledepth = %d\n",(int)value); - wavinfo.sampledepth=(int)value; + wavinfo.sampledepth = use_dsp ? 16 : (int)value; break; case DSP_SET_STEREO_MODE: DEBUGF("Stereo mode = %d\n",(int)value); - wavinfo.stereomode=(int)value; + wavinfo.stereomode = use_dsp ? STEREO_INTERLEAVED : (int)value; break; } @@ -698,8 +689,10 @@ static enum plugin_status test_track(const char* filename) ci.id3 = &track.id3; ci.curpos = 0; - if (use_dsp) + if (use_dsp) { rb->dsp_configure(ci.dsp, DSP_RESET, 0); + rb->dsp_configure(ci.dsp, DSP_FLUSH, 0); + } if (checksum) crc32 = 0xffffffff;