![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Production Test Program (PTP) for the LPC4088 Experiment Base Board
Dependencies: EALib I2S LM75B SDFileSystem mbed
TestAudio.cpp@5:3eb61b96b6ac, 2014-09-09 (annotated)
- Committer:
- embeddedartists
- Date:
- Tue Sep 09 09:58:19 2014 +0000
- Revision:
- 5:3eb61b96b6ac
- Parent:
- 4:aa34674b6afb
- Child:
- 6:4d97917c7dea
Corrected spelling mistake (andro)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 2:2f4b7535ceb3 | 1 | /* |
embeddedartists | 2:2f4b7535ceb3 | 2 | * Copyright 2013 Embedded Artists AB |
embeddedartists | 2:2f4b7535ceb3 | 3 | * |
embeddedartists | 2:2f4b7535ceb3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
embeddedartists | 2:2f4b7535ceb3 | 5 | * you may not use this file except in compliance with the License. |
embeddedartists | 2:2f4b7535ceb3 | 6 | * You may obtain a copy of the License at |
embeddedartists | 2:2f4b7535ceb3 | 7 | * |
embeddedartists | 2:2f4b7535ceb3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
embeddedartists | 2:2f4b7535ceb3 | 9 | * |
embeddedartists | 2:2f4b7535ceb3 | 10 | * Unless required by applicable law or agreed to in writing, software |
embeddedartists | 2:2f4b7535ceb3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
embeddedartists | 2:2f4b7535ceb3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
embeddedartists | 2:2f4b7535ceb3 | 13 | * See the License for the specific language governing permissions and |
embeddedartists | 2:2f4b7535ceb3 | 14 | * limitations under the License. |
embeddedartists | 2:2f4b7535ceb3 | 15 | */ |
embeddedartists | 2:2f4b7535ceb3 | 16 | |
embeddedartists | 2:2f4b7535ceb3 | 17 | /****************************************************************************** |
embeddedartists | 2:2f4b7535ceb3 | 18 | * Includes |
embeddedartists | 2:2f4b7535ceb3 | 19 | *****************************************************************************/ |
embeddedartists | 4:aa34674b6afb | 20 | //#define SOUND_8KHZ |
embeddedartists | 2:2f4b7535ceb3 | 21 | |
embeddedartists | 2:2f4b7535ceb3 | 22 | #include "mbed.h" |
embeddedartists | 2:2f4b7535ceb3 | 23 | #include "TestAudio.h" |
embeddedartists | 2:2f4b7535ceb3 | 24 | #include "WM8731.h" |
embeddedartists | 3:7ef908e84ae1 | 25 | #include "I2S.h" |
embeddedartists | 3:7ef908e84ae1 | 26 | #include "sound.h" |
embeddedartists | 2:2f4b7535ceb3 | 27 | |
embeddedartists | 2:2f4b7535ceb3 | 28 | /****************************************************************************** |
embeddedartists | 2:2f4b7535ceb3 | 29 | * Defines and typedefs |
embeddedartists | 2:2f4b7535ceb3 | 30 | *****************************************************************************/ |
embeddedartists | 2:2f4b7535ceb3 | 31 | |
embeddedartists | 3:7ef908e84ae1 | 32 | #define SAMPLERATE 32000 |
embeddedartists | 3:7ef908e84ae1 | 33 | |
embeddedartists | 3:7ef908e84ae1 | 34 | /****************************************************************************** |
embeddedartists | 3:7ef908e84ae1 | 35 | * Private Functions |
embeddedartists | 3:7ef908e84ae1 | 36 | *****************************************************************************/ |
embeddedartists | 3:7ef908e84ae1 | 37 | |
embeddedartists | 3:7ef908e84ae1 | 38 | void TestAudio::echo(void) { |
embeddedartists | 3:7ef908e84ae1 | 39 | int to_read = _i2sRx.fifo_points(); //Find out how many stereo samples to read |
embeddedartists | 3:7ef908e84ae1 | 40 | |
embeddedartists | 3:7ef908e84ae1 | 41 | //Read 'to_read' number of stereo samples. |
embeddedartists | 3:7ef908e84ae1 | 42 | _i2sRx.read(_rxBuf, to_read); |
embeddedartists | 3:7ef908e84ae1 | 43 | |
embeddedartists | 3:7ef908e84ae1 | 44 | //Loop through all stereo samples |
embeddedartists | 3:7ef908e84ae1 | 45 | for(int i = 0; i < to_read; i+=2) { |
embeddedartists | 3:7ef908e84ae1 | 46 | // Check if wave-file shall be output |
embeddedartists | 3:7ef908e84ae1 | 47 | if (_waveIdx > 0) |
embeddedartists | 3:7ef908e84ae1 | 48 | { |
embeddedartists | 4:aa34674b6afb | 49 | #if SOUND_8KHZ |
embeddedartists | 4:aa34674b6afb | 50 | int sample = sound_8k[_waveIdx/4] << 5 |
embeddedartists | 4:aa34674b6afb | 51 | _txBuf[i] = sample; |
embeddedartists | 4:aa34674b6afb | 52 | _txBuf[i+1] = sample; |
embeddedartists | 4:aa34674b6afb | 53 | _echoBuf[_echoBufPtr] = _rxBuf[i]; |
embeddedartists | 3:7ef908e84ae1 | 54 | |
embeddedartists | 3:7ef908e84ae1 | 55 | //Increment wave samples pointer and check if all samples have been read, if so, set pointer to zero |
embeddedartists | 3:7ef908e84ae1 | 56 | _waveIdx++; |
embeddedartists | 3:7ef908e84ae1 | 57 | if (_waveIdx/4 > sound_sz) |
embeddedartists | 3:7ef908e84ae1 | 58 | { |
embeddedartists | 3:7ef908e84ae1 | 59 | _waveIdx = 0; |
embeddedartists | 3:7ef908e84ae1 | 60 | } |
embeddedartists | 4:aa34674b6afb | 61 | #else |
embeddedartists | 4:aa34674b6afb | 62 | int sample = (unsigned int)sound_32k[_waveIdx] | (((unsigned int)sound_32k[_waveIdx+1]) << 8); |
embeddedartists | 4:aa34674b6afb | 63 | _txBuf[i] = sample; |
embeddedartists | 4:aa34674b6afb | 64 | _txBuf[i+1] = sample; |
embeddedartists | 4:aa34674b6afb | 65 | _echoBuf[_echoBufPtr] = _rxBuf[i]; |
embeddedartists | 4:aa34674b6afb | 66 | |
embeddedartists | 4:aa34674b6afb | 67 | //Increment wave samples pointer and check if all samples have been read, if so, set pointer to zero |
embeddedartists | 4:aa34674b6afb | 68 | _waveIdx += 2; |
embeddedartists | 4:aa34674b6afb | 69 | if (_waveIdx > sound_sz) |
embeddedartists | 4:aa34674b6afb | 70 | { |
embeddedartists | 4:aa34674b6afb | 71 | _waveIdx = 0; |
embeddedartists | 4:aa34674b6afb | 72 | } |
embeddedartists | 4:aa34674b6afb | 73 | #endif |
embeddedartists | 3:7ef908e84ae1 | 74 | } |
embeddedartists | 3:7ef908e84ae1 | 75 | //Output delayed version of audio input |
embeddedartists | 3:7ef908e84ae1 | 76 | else |
embeddedartists | 3:7ef908e84ae1 | 77 | { |
embeddedartists | 3:7ef908e84ae1 | 78 | //Echo buffer only contains right side samples (since MIC is used as input, which is a mono input) |
embeddedartists | 3:7ef908e84ae1 | 79 | //Output is however stereo, so just copy echo bugger to both right and left side for output samples |
embeddedartists | 3:7ef908e84ae1 | 80 | _txBuf[i] = _echoBuf[_echoBufPtr]; |
embeddedartists | 3:7ef908e84ae1 | 81 | _txBuf[i+1] = _echoBuf[_echoBufPtr]; |
embeddedartists | 3:7ef908e84ae1 | 82 | |
embeddedartists | 3:7ef908e84ae1 | 83 | //Only fill echo buffer with right side samples, i.e., ignore rxBuf[i+1] content |
embeddedartists | 3:7ef908e84ae1 | 84 | _echoBuf[_echoBufPtr] = _rxBuf[i]; |
embeddedartists | 3:7ef908e84ae1 | 85 | } |
embeddedartists | 3:7ef908e84ae1 | 86 | |
embeddedartists | 3:7ef908e84ae1 | 87 | //Increment echo buffer write pointer and check for wrap-around |
embeddedartists | 3:7ef908e84ae1 | 88 | _echoBufPtr++; |
embeddedartists | 3:7ef908e84ae1 | 89 | if (_echoBufPtr >= ECHOLENGTH) |
embeddedartists | 3:7ef908e84ae1 | 90 | { |
embeddedartists | 3:7ef908e84ae1 | 91 | _echoBufPtr = 0; |
embeddedartists | 3:7ef908e84ae1 | 92 | } |
embeddedartists | 3:7ef908e84ae1 | 93 | } |
embeddedartists | 3:7ef908e84ae1 | 94 | |
embeddedartists | 3:7ef908e84ae1 | 95 | //Write samples to output/codec |
embeddedartists | 3:7ef908e84ae1 | 96 | _i2sTx.write(_txBuf, to_read); |
embeddedartists | 3:7ef908e84ae1 | 97 | } |
embeddedartists | 3:7ef908e84ae1 | 98 | |
embeddedartists | 2:2f4b7535ceb3 | 99 | /****************************************************************************** |
embeddedartists | 2:2f4b7535ceb3 | 100 | * Public Functions |
embeddedartists | 2:2f4b7535ceb3 | 101 | *****************************************************************************/ |
embeddedartists | 2:2f4b7535ceb3 | 102 | |
embeddedartists | 2:2f4b7535ceb3 | 103 | /* |
embeddedartists | 2:2f4b7535ceb3 | 104 | Prerequisites: |
embeddedartists | 2:2f4b7535ceb3 | 105 | |
embeddedartists | 2:2f4b7535ceb3 | 106 | - For this test to work jumpers JP8 and JP9 on the LPC4088 Experiment Base Board |
embeddedartists | 2:2f4b7535ceb3 | 107 | must both be in positions 1-2 |
embeddedartists | 2:2f4b7535ceb3 | 108 | |
embeddedartists | 2:2f4b7535ceb3 | 109 | - MIC cable, Line OUT, Headphone... |
embeddedartists | 2:2f4b7535ceb3 | 110 | */ |
embeddedartists | 2:2f4b7535ceb3 | 111 | |
embeddedartists | 3:7ef908e84ae1 | 112 | TestAudio::TestAudio() : _codec(P0_27, P0_28), _i2sTx(I2S_TRANSMIT, p11, p12, p13), |
embeddedartists | 3:7ef908e84ae1 | 113 | _i2sRx(I2S_RECEIVE, p14, p33, p34),_aIn(p15),_waveIdx(0),_echoBufPtr(0) { |
embeddedartists | 3:7ef908e84ae1 | 114 | } |
embeddedartists | 3:7ef908e84ae1 | 115 | |
embeddedartists | 2:2f4b7535ceb3 | 116 | bool TestAudio::runTest() { |
embeddedartists | 4:aa34674b6afb | 117 | printf("Testing audio... Exit by pressing joystick up\n"); |
embeddedartists | 5:3eb61b96b6ac | 118 | printf("Joystick down = play short wave-file. Joystick right/left = turn microphone boost on/off\n"); |
embeddedartists | 3:7ef908e84ae1 | 119 | |
embeddedartists | 3:7ef908e84ae1 | 120 | // joystick used for audio selection |
embeddedartists | 3:7ef908e84ae1 | 121 | DigitalIn up(p32); |
embeddedartists | 3:7ef908e84ae1 | 122 | DigitalIn down(p38); |
embeddedartists | 4:aa34674b6afb | 123 | DigitalIn left(p39); |
embeddedartists | 4:aa34674b6afb | 124 | DigitalIn right(p37); |
embeddedartists | 3:7ef908e84ae1 | 125 | |
embeddedartists | 3:7ef908e84ae1 | 126 | _codec.power(true); |
embeddedartists | 3:7ef908e84ae1 | 127 | _codec.frequency(SAMPLERATE); |
embeddedartists | 3:7ef908e84ae1 | 128 | _codec.wordsize(16); |
embeddedartists | 3:7ef908e84ae1 | 129 | _codec.master(true); |
embeddedartists | 3:7ef908e84ae1 | 130 | _codec.headphone_volume(0.1); |
embeddedartists | 3:7ef908e84ae1 | 131 | _codec.input_select(WM8731_MIC); |
embeddedartists | 4:aa34674b6afb | 132 | _codec.microphone_boost(true); |
embeddedartists | 3:7ef908e84ae1 | 133 | _codec.input_mute(false); |
embeddedartists | 3:7ef908e84ae1 | 134 | _codec.output_mute(false); |
embeddedartists | 3:7ef908e84ae1 | 135 | _codec.input_power(true); |
embeddedartists | 3:7ef908e84ae1 | 136 | _codec.output_power(true); |
embeddedartists | 3:7ef908e84ae1 | 137 | _codec.linein_volume(0.7); |
embeddedartists | 3:7ef908e84ae1 | 138 | _codec.bypass(true); |
embeddedartists | 3:7ef908e84ae1 | 139 | _codec.start(); |
embeddedartists | 3:7ef908e84ae1 | 140 | |
embeddedartists | 4:aa34674b6afb | 141 | _i2sRx.frequency(SAMPLERATE); |
embeddedartists | 4:aa34674b6afb | 142 | _i2sRx.wordsize(16); |
embeddedartists | 4:aa34674b6afb | 143 | _i2sRx.stereomono(I2S_STEREO); |
embeddedartists | 4:aa34674b6afb | 144 | _i2sRx.masterslave(I2S_SLAVE); |
embeddedartists | 4:aa34674b6afb | 145 | _i2sRx.attach(this, &TestAudio::echo); |
embeddedartists | 4:aa34674b6afb | 146 | _i2sRx.set_interrupt_fifo_level(1); |
embeddedartists | 4:aa34674b6afb | 147 | _echoBufPtr = 0; |
embeddedartists | 4:aa34674b6afb | 148 | _waveIdx = 0; |
embeddedartists | 4:aa34674b6afb | 149 | _i2sRx.start(); |
embeddedartists | 4:aa34674b6afb | 150 | |
embeddedartists | 3:7ef908e84ae1 | 151 | _i2sTx.frequency(SAMPLERATE); |
embeddedartists | 3:7ef908e84ae1 | 152 | _i2sTx.wordsize(16); |
embeddedartists | 3:7ef908e84ae1 | 153 | _i2sTx.stereomono(I2S_STEREO); |
embeddedartists | 3:7ef908e84ae1 | 154 | _i2sTx.masterslave(I2S_SLAVE); |
embeddedartists | 3:7ef908e84ae1 | 155 | _i2sTx.start(); |
embeddedartists | 3:7ef908e84ae1 | 156 | |
embeddedartists | 3:7ef908e84ae1 | 157 | |
embeddedartists | 3:7ef908e84ae1 | 158 | do |
embeddedartists | 3:7ef908e84ae1 | 159 | { |
embeddedartists | 3:7ef908e84ae1 | 160 | //check if joystick-down pressed = play wave-file |
embeddedartists | 3:7ef908e84ae1 | 161 | if ((down.read() == 0) && (_waveIdx == 0)) |
embeddedartists | 4:aa34674b6afb | 162 | _waveIdx = 2; |
embeddedartists | 4:aa34674b6afb | 163 | |
embeddedartists | 4:aa34674b6afb | 164 | //check if joystick-left pressed = turn off microphone boost |
embeddedartists | 4:aa34674b6afb | 165 | if (left.read() == 0) |
embeddedartists | 4:aa34674b6afb | 166 | { |
embeddedartists | 4:aa34674b6afb | 167 | _codec.input_mute(true); |
embeddedartists | 4:aa34674b6afb | 168 | wait(0.2); |
embeddedartists | 4:aa34674b6afb | 169 | _codec.microphone_boost(false); |
embeddedartists | 4:aa34674b6afb | 170 | wait(0.2); |
embeddedartists | 4:aa34674b6afb | 171 | _codec.input_mute(false); |
embeddedartists | 4:aa34674b6afb | 172 | } |
embeddedartists | 4:aa34674b6afb | 173 | |
embeddedartists | 4:aa34674b6afb | 174 | //check if joystick-right pressed = turn on microphone boost |
embeddedartists | 4:aa34674b6afb | 175 | if (right.read() == 0) |
embeddedartists | 4:aa34674b6afb | 176 | { |
embeddedartists | 4:aa34674b6afb | 177 | _codec.input_mute(true); |
embeddedartists | 4:aa34674b6afb | 178 | wait(0.2); |
embeddedartists | 4:aa34674b6afb | 179 | _codec.microphone_boost(true); |
embeddedartists | 4:aa34674b6afb | 180 | wait(0.2); |
embeddedartists | 4:aa34674b6afb | 181 | _codec.input_mute(false); |
embeddedartists | 4:aa34674b6afb | 182 | } |
embeddedartists | 3:7ef908e84ae1 | 183 | |
embeddedartists | 3:7ef908e84ae1 | 184 | _codec.headphone_volume(_aIn); |
embeddedartists | 4:aa34674b6afb | 185 | wait(0.25); |
embeddedartists | 3:7ef908e84ae1 | 186 | } while (up.read() != 0); |
embeddedartists | 4:aa34674b6afb | 187 | |
embeddedartists | 4:aa34674b6afb | 188 | _codec.power(false); |
embeddedartists | 2:2f4b7535ceb3 | 189 | return true; |
embeddedartists | 2:2f4b7535ceb3 | 190 | } |
embeddedartists | 2:2f4b7535ceb3 | 191 | |
embeddedartists | 2:2f4b7535ceb3 | 192 |