Code for measuring the signal with a specified length and sampling rate, and saving it on a SD card.

Dependencies:   EALib I2S mbed

Committer:
msamadani
Date:
Thu Jul 13 19:33:37 2017 +0000
Revision:
0:c05b00be2229
bpFilter function works.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
msamadani 0:c05b00be2229 1 /*
msamadani 0:c05b00be2229 2 * Copyright 2013 Embedded Artists AB
msamadani 0:c05b00be2229 3 *
msamadani 0:c05b00be2229 4 * Licensed under the Apache License, Version 2.0 (the "License");
msamadani 0:c05b00be2229 5 * you may not use this file except in compliance with the License.
msamadani 0:c05b00be2229 6 * You may obtain a copy of the License at
msamadani 0:c05b00be2229 7 *
msamadani 0:c05b00be2229 8 * http://www.apache.org/licenses/LICENSE-2.0
msamadani 0:c05b00be2229 9 *
msamadani 0:c05b00be2229 10 * Unless required by applicable law or agreed to in writing, software
msamadani 0:c05b00be2229 11 * distributed under the License is distributed on an "AS IS" BASIS,
msamadani 0:c05b00be2229 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
msamadani 0:c05b00be2229 13 * See the License for the specific language governing permissions and
msamadani 0:c05b00be2229 14 * limitations under the License.
msamadani 0:c05b00be2229 15 */
msamadani 0:c05b00be2229 16
msamadani 0:c05b00be2229 17 /******************************************************************************
msamadani 0:c05b00be2229 18 * Includes
msamadani 0:c05b00be2229 19 *****************************************************************************/
msamadani 0:c05b00be2229 20 //#define SOUND_8KHZ
msamadani 0:c05b00be2229 21
msamadani 0:c05b00be2229 22 #include "mbed.h"
msamadani 0:c05b00be2229 23 #include "TestAudio.h"
msamadani 0:c05b00be2229 24 #include "WM8731.h"
msamadani 0:c05b00be2229 25 #include "I2S.h"
msamadani 0:c05b00be2229 26 //#include "sound.h"
msamadani 0:c05b00be2229 27
msamadani 0:c05b00be2229 28
msamadani 0:c05b00be2229 29 /******************************************************************************
msamadani 0:c05b00be2229 30 * Defines and typedefs
msamadani 0:c05b00be2229 31 *****************************************************************************/
msamadani 0:c05b00be2229 32
msamadani 0:c05b00be2229 33 #define SAMPLERATE 32000
msamadani 0:c05b00be2229 34
msamadani 0:c05b00be2229 35
msamadani 0:c05b00be2229 36 /******************************************************************************
msamadani 0:c05b00be2229 37 * Private Functions
msamadani 0:c05b00be2229 38 *****************************************************************************/
msamadani 0:c05b00be2229 39
msamadani 0:c05b00be2229 40 void TestAudio::echo(void) {
msamadani 0:c05b00be2229 41 int to_read = _i2sRx.fifo_points(); //Find out how many stereo samples to read
msamadani 0:c05b00be2229 42 //printf("%d\n\n",to_read);
msamadani 0:c05b00be2229 43
msamadani 0:c05b00be2229 44 //Read 'to_read' number of stereo samples.
msamadani 0:c05b00be2229 45 _i2sRx.read(_rxBuf, to_read);
msamadani 0:c05b00be2229 46 //printf("%d\n\n",_rxBuf);
msamadani 0:c05b00be2229 47
msamadani 0:c05b00be2229 48 //Loop through all stereo samples
msamadani 0:c05b00be2229 49 for(int i = 0; i < to_read; i+=2) {
msamadani 0:c05b00be2229 50 //printf("%d\n\n",i);
msamadani 0:c05b00be2229 51
msamadani 0:c05b00be2229 52
msamadani 0:c05b00be2229 53 //Only fill echo buffer with right side samples, i.e., ignore rxBuf[i+1] content
msamadani 0:c05b00be2229 54 _echoBuf[_echoBufPtr] = _rxBuf[i];
msamadani 0:c05b00be2229 55 //printf("%d\n\n",_echoBuf[_echoBufPtr]);
msamadani 0:c05b00be2229 56 //Increment echo buffer write pointer and check for wrap-around
msamadani 0:c05b00be2229 57 _echoBufPtr++;
msamadani 0:c05b00be2229 58 if (_echoBufPtr >= ECHOLENGTH)
msamadani 0:c05b00be2229 59 {
msamadani 0:c05b00be2229 60 _echoBufPtr = 0;
msamadani 0:c05b00be2229 61 }
msamadani 0:c05b00be2229 62 }
msamadani 0:c05b00be2229 63
msamadani 0:c05b00be2229 64 //Write samples to output/codec
msamadani 0:c05b00be2229 65 _i2sTx.write(_txBuf, to_read);
msamadani 0:c05b00be2229 66 // printf("%d\n\n",sizeof(_txBuf));
msamadani 0:c05b00be2229 67 }
msamadani 0:c05b00be2229 68
msamadani 0:c05b00be2229 69 /******************************************************************************
msamadani 0:c05b00be2229 70 * Public Functions
msamadani 0:c05b00be2229 71 *****************************************************************************/
msamadani 0:c05b00be2229 72
msamadani 0:c05b00be2229 73 /*
msamadani 0:c05b00be2229 74 Prerequisites:
msamadani 0:c05b00be2229 75
msamadani 0:c05b00be2229 76 - For this test to work jumpers JP8 and JP9 on the LPC4088 Experiment Base Board
msamadani 0:c05b00be2229 77 must both be in positions 1-2
msamadani 0:c05b00be2229 78
msamadani 0:c05b00be2229 79 - MIC cable, Line OUT, Headphone...
msamadani 0:c05b00be2229 80 */
msamadani 0:c05b00be2229 81
msamadani 0:c05b00be2229 82 TestAudio::TestAudio() : _codec(P0_27, P0_28), _i2sTx(I2S_TRANSMIT, p11, p12, p13),
msamadani 0:c05b00be2229 83 _i2sRx(I2S_RECEIVE, p14, p33, p34),_aIn(p15),_waveIdx(0),_echoBufPtr(0) {
msamadani 0:c05b00be2229 84 }
msamadani 0:c05b00be2229 85
msamadani 0:c05b00be2229 86 bool TestAudio::runTest() {
msamadani 0:c05b00be2229 87 printf("Testing audio... Exit by pressing joystick up\n");
msamadani 0:c05b00be2229 88 printf("Joystick down = play short wave-file. Joystick right/left = turn microphone boost on/off\n");
msamadani 0:c05b00be2229 89
msamadani 0:c05b00be2229 90 // joystick used for audio selection
msamadani 0:c05b00be2229 91 DigitalIn up(p32);
msamadani 0:c05b00be2229 92 DigitalIn down(p38);
msamadani 0:c05b00be2229 93 DigitalIn left(p39);
msamadani 0:c05b00be2229 94 DigitalIn right(p37);
msamadani 0:c05b00be2229 95
msamadani 0:c05b00be2229 96 for(int i=0; i<ECHOLENGTH; i++)
msamadani 0:c05b00be2229 97 _echoBuf[i] = 0;
msamadani 0:c05b00be2229 98
msamadani 0:c05b00be2229 99 _codec.power(true);
msamadani 0:c05b00be2229 100 _codec.frequency(SAMPLERATE);
msamadani 0:c05b00be2229 101 _codec.wordsize(16);
msamadani 0:c05b00be2229 102 _codec.master(true);
msamadani 0:c05b00be2229 103 _codec.headphone_volume(0.1);
msamadani 0:c05b00be2229 104 _codec.input_select(WM8731_MIC);
msamadani 0:c05b00be2229 105 _codec.microphone_boost(true);
msamadani 0:c05b00be2229 106 _codec.input_mute(false);
msamadani 0:c05b00be2229 107 _codec.output_mute(false);
msamadani 0:c05b00be2229 108 _codec.input_power(true);
msamadani 0:c05b00be2229 109 _codec.output_power(true);
msamadani 0:c05b00be2229 110 _codec.linein_volume(0.7);
msamadani 0:c05b00be2229 111 _codec.bypass(true);
msamadani 0:c05b00be2229 112 _codec.start();
msamadani 0:c05b00be2229 113
msamadani 0:c05b00be2229 114 _i2sRx.frequency(SAMPLERATE);
msamadani 0:c05b00be2229 115 _i2sRx.wordsize(16);
msamadani 0:c05b00be2229 116 _i2sRx.stereomono(I2S_MONO);
msamadani 0:c05b00be2229 117 _i2sRx.masterslave(I2S_SLAVE);
msamadani 0:c05b00be2229 118 _i2sRx.attach(this, &TestAudio::echo);
msamadani 0:c05b00be2229 119 _i2sRx.set_interrupt_fifo_level(1);
msamadani 0:c05b00be2229 120 _echoBufPtr = 0;
msamadani 0:c05b00be2229 121 _waveIdx = 0;
msamadani 0:c05b00be2229 122 _i2sRx.start();
msamadani 0:c05b00be2229 123
msamadani 0:c05b00be2229 124 _i2sTx.frequency(SAMPLERATE);
msamadani 0:c05b00be2229 125 _i2sTx.wordsize(16);
msamadani 0:c05b00be2229 126 _i2sTx.stereomono(I2S_MONO);
msamadani 0:c05b00be2229 127 _i2sTx.masterslave(I2S_SLAVE);
msamadani 0:c05b00be2229 128 _i2sTx.start();
msamadani 0:c05b00be2229 129
msamadani 0:c05b00be2229 130 int lastVol = -1000; // value not important
msamadani 0:c05b00be2229 131 do
msamadani 0:c05b00be2229 132 {
msamadani 0:c05b00be2229 133 //check if joystick-down pressed = play wave-file
msamadani 0:c05b00be2229 134 if ((down.read() == 0) && (_waveIdx == 0))
msamadani 0:c05b00be2229 135 _waveIdx = 2;
msamadani 0:c05b00be2229 136
msamadani 0:c05b00be2229 137 //check if joystick-left pressed = turn off microphone boost
msamadani 0:c05b00be2229 138 if (left.read() == 0)
msamadani 0:c05b00be2229 139 {
msamadani 0:c05b00be2229 140 _codec.input_mute(true);
msamadani 0:c05b00be2229 141 wait(0.2);
msamadani 0:c05b00be2229 142 _codec.microphone_boost(false);
msamadani 0:c05b00be2229 143 wait(0.2);
msamadani 0:c05b00be2229 144 _codec.input_mute(false);
msamadani 0:c05b00be2229 145 }
msamadani 0:c05b00be2229 146
msamadani 0:c05b00be2229 147 //check if joystick-right pressed = turn on microphone boost
msamadani 0:c05b00be2229 148 if (right.read() == 0)
msamadani 0:c05b00be2229 149 {
msamadani 0:c05b00be2229 150 _codec.input_mute(true);
msamadani 0:c05b00be2229 151 wait(0.2);
msamadani 0:c05b00be2229 152 _codec.microphone_boost(true);
msamadani 0:c05b00be2229 153 wait(0.2);
msamadani 0:c05b00be2229 154 _codec.input_mute(false);
msamadani 0:c05b00be2229 155 }
msamadani 0:c05b00be2229 156
msamadani 0:c05b00be2229 157
msamadani 0:c05b00be2229 158 //attempt to filter out the small variations in analog values and
msamadani 0:c05b00be2229 159 //only update volume when a "large enough" change is detected.
msamadani 0:c05b00be2229 160 float v = _aIn;
msamadani 0:c05b00be2229 161 int val = ((int)(v*1000))>>2;
msamadani 0:c05b00be2229 162 if (val != lastVol) {
msamadani 0:c05b00be2229 163 _codec.headphone_volume(v);
msamadani 0:c05b00be2229 164 lastVol = val;
msamadani 0:c05b00be2229 165 }
msamadani 0:c05b00be2229 166
msamadani 0:c05b00be2229 167
msamadani 0:c05b00be2229 168 //_codec.headphone_volume(_aIn);
msamadani 0:c05b00be2229 169 wait(0.25);
msamadani 0:c05b00be2229 170 } while (up.read() != 0);
msamadani 0:c05b00be2229 171
msamadani 0:c05b00be2229 172 _codec.power(false);
msamadani 0:c05b00be2229 173 return true;
msamadani 0:c05b00be2229 174 }
msamadani 0:c05b00be2229 175
msamadani 0:c05b00be2229 176