Production Test Program (PTP) for the LPC4088 Experiment Base Board

Dependencies:   EALib I2S LM75B SDFileSystem mbed

TestAudio.cpp

Committer:
embeddedartists
Date:
2014-09-08
Revision:
3:7ef908e84ae1
Parent:
2:2f4b7535ceb3
Child:
4:aa34674b6afb

File content as of revision 3:7ef908e84ae1:

/*
 *  Copyright 2013 Embedded Artists AB
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/******************************************************************************
 * Includes
 *****************************************************************************/

#include "mbed.h"
#include "TestAudio.h"
#include "WM8731.h"
#include "I2S.h"
#include "sound.h"

/******************************************************************************
 * Defines and typedefs
 *****************************************************************************/

#define SAMPLERATE 32000

/******************************************************************************
 * Private Functions
 *****************************************************************************/

void TestAudio::echo(void) {
    int to_read = _i2sRx.fifo_points();  //Find out how many stereo samples to read
    
    //Read 'to_read' number of stereo samples.
    _i2sRx.read(_rxBuf, to_read);
    
    //Loop through all stereo samples
    for(int i = 0; i < to_read; i+=2) {
        // Check if wave-file shall be output
        if (_waveIdx > 0)
        {
            _txBuf[i]   = sound_8k[_waveIdx/4] << 5;
            _txBuf[i+1] = sound_8k[_waveIdx/4] << 5;

            //Increment wave samples pointer and check if all samples have been read, if so, set pointer to zero
            _waveIdx++;
            if (_waveIdx/4 > sound_sz)
            {
                _waveIdx = 0;
            }
        }
        //Output delayed version of audio input
        else
        {
            //Echo buffer only contains right side samples (since MIC is used as input, which is a mono input)
            //Output is however stereo, so just copy echo bugger to both right and left side for output samples
            _txBuf[i]   = _echoBuf[_echoBufPtr];
            _txBuf[i+1] = _echoBuf[_echoBufPtr];

            //Only fill echo buffer with right side samples, i.e., ignore rxBuf[i+1] content
            _echoBuf[_echoBufPtr]   = _rxBuf[i];
        }

        //Increment echo buffer write pointer and check for wrap-around
        _echoBufPtr++;
        if (_echoBufPtr >= ECHOLENGTH)
        {
            _echoBufPtr = 0;
        }
    }
    
    //Write samples to output/codec
    _i2sTx.write(_txBuf, to_read);
}

/******************************************************************************
 * Public Functions
 *****************************************************************************/

/*
   Prerequisites:
 
   - For this test to work jumpers JP8 and JP9 on the LPC4088 Experiment Base Board
     must both be in positions 1-2
     
   - MIC cable, Line OUT, Headphone...
*/

TestAudio::TestAudio() : _codec(P0_27, P0_28), _i2sTx(I2S_TRANSMIT, p11, p12, p13),
    _i2sRx(I2S_RECEIVE, p14, p33, p34),_aIn(p15),_waveIdx(0),_echoBufPtr(0) {
}

bool TestAudio::runTest() {
    printf("Testing audio...\n");

    // joystick used for audio selection    
    DigitalIn up(p32);
    DigitalIn down(p38);
    
    _codec.power(true);
    _codec.frequency(SAMPLERATE);
    _codec.wordsize(16);  
    _codec.master(true);
    _codec.headphone_volume(0.1);
    _codec.input_select(WM8731_MIC);
    _codec.microphone_boost(false);
    _codec.input_mute(false);
    _codec.output_mute(false);
    _codec.input_power(true);
    _codec.output_power(true);
//    _codec.sidetone(0.99);
    _codec.linein_volume(0.7);
    _codec.bypass(true);
    _codec.start();

    _i2sTx.frequency(SAMPLERATE);
    _i2sTx.wordsize(16);
    _i2sTx.stereomono(I2S_STEREO);
    _i2sTx.masterslave(I2S_SLAVE);
    _i2sTx.start();

    _i2sRx.frequency(SAMPLERATE);
    _i2sRx.wordsize(16);
    _i2sRx.stereomono(I2S_STEREO);
    _i2sRx.masterslave(I2S_SLAVE);
    _i2sRx.attach(this, &TestAudio::echo);
    _echoBufPtr = 0;
    _waveIdx = 0;
    _i2sRx.start();
    
    do
    {
        //check if joystick-down pressed = play wave-file
        if ((down.read() == 0) && (_waveIdx == 0))
            _waveIdx = 1;

        _codec.headphone_volume(_aIn);
        wait(0.05);
    } while (up.read() != 0);
    
    return true;
}