Example application which combines the STMicroelectronics X-NUCLEO-CCA01M1 Sound Terminal Expansion Board and the X-NUCLEO-CCA02M1 MEMS Microphones Expansion Board to realize a simple stereo@32KHz Karaoke system. It is built against mbed OS.

Dependencies:   X_NUCLEO_CCA02M1 X_NUCLEO_CCA01M1

Having fun with Karaoke

This application provides an example of usage of the X-NUCLEO-CCA01M1 Sound Terminal Expansion Board and the X-NUCLEO-CCA02M1 MEMS Microphones Expansion Board on top of a Nucleo-F401RE board to realize a simple stereo@32KHz Karaoke system: it records the audio through the microphones and plays it through the speakers on the fly. It is built against mbed OS.

/media/uploads/Davidroid/karaoke.jpg


Power supply

The X-NUCLEO-CCA01M1 Sound Terminal Expansion Board has to be powered with at least 5V DC. You can connect the VCC terminal to an external power supplier or directly to the +5V pin of the CN6 Arduino connector.


SW Configuration

Currently the only configuration supported is stereo@32KHz for both boards.


HW Configuration

To make the two audio boards work together, the X-NUCLEO-CCA01M1 Sound Terminal Expansion Board has to be configured to use the I2S2 peripheral of the Nucleo F401RE board: please refer to the X_NUCLEO_CCA01M1 page.

Committer:
Davidroid
Date:
Wed Dec 12 16:30:32 2018 +0000
Revision:
8:f016c6826ab3
Parent:
0:ccce492abbb4
Update with the new version of the libraries

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:ccce492abbb4 1 /**
Davidroid 0:ccce492abbb4 2 ******************************************************************************
Davidroid 0:ccce492abbb4 3 * @file main.cpp
Davidroid 0:ccce492abbb4 4 * @author Davide Aliprandi, STMicroelectronics
Davidroid 0:ccce492abbb4 5 * @version V1.0.0
Davidroid 0:ccce492abbb4 6 * @date November 25th, 2016
Davidroid 0:ccce492abbb4 7 * @brief mbed test application which combines the STMicroelectronics
Davidroid 0:ccce492abbb4 8 * X-NUCLEO-CCA01M1 and the X-NUCLEO-CCA02M1 Expansion Boards.
Davidroid 0:ccce492abbb4 9 ******************************************************************************
Davidroid 0:ccce492abbb4 10 * @attention
Davidroid 0:ccce492abbb4 11 *
Davidroid 0:ccce492abbb4 12 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
Davidroid 0:ccce492abbb4 13 *
Davidroid 0:ccce492abbb4 14 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:ccce492abbb4 15 * are permitted provided that the following conditions are met:
Davidroid 0:ccce492abbb4 16 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:ccce492abbb4 17 * this list of conditions and the following disclaimer.
Davidroid 0:ccce492abbb4 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:ccce492abbb4 19 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:ccce492abbb4 20 * and/or other materials provided with the distribution.
Davidroid 0:ccce492abbb4 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:ccce492abbb4 22 * may be used to endorse or promote products derived from this software
Davidroid 0:ccce492abbb4 23 * without specific prior written permission.
Davidroid 0:ccce492abbb4 24 *
Davidroid 0:ccce492abbb4 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:ccce492abbb4 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:ccce492abbb4 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:ccce492abbb4 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:ccce492abbb4 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:ccce492abbb4 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:ccce492abbb4 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:ccce492abbb4 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:ccce492abbb4 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:ccce492abbb4 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:ccce492abbb4 35 *
Davidroid 0:ccce492abbb4 36 ******************************************************************************
Davidroid 0:ccce492abbb4 37 */
Davidroid 0:ccce492abbb4 38
Davidroid 0:ccce492abbb4 39
Davidroid 0:ccce492abbb4 40 /* Includes ------------------------------------------------------------------*/
Davidroid 0:ccce492abbb4 41
Davidroid 0:ccce492abbb4 42 /* mbed specific header files. */
Davidroid 0:ccce492abbb4 43 #include "mbed.h"
Davidroid 0:ccce492abbb4 44 #include "rtos.h"
Davidroid 0:ccce492abbb4 45
Davidroid 0:ccce492abbb4 46
Davidroid 0:ccce492abbb4 47 #if DEVICE_I2S
Davidroid 0:ccce492abbb4 48
Davidroid 0:ccce492abbb4 49 /* Helper header files. */
Davidroid 0:ccce492abbb4 50 #include "DevI2C.h"
Davidroid 0:ccce492abbb4 51
Davidroid 0:ccce492abbb4 52 /* Component specific header files. */
Davidroid 0:ccce492abbb4 53 #include "STA350BW.h"
Davidroid 0:ccce492abbb4 54
Davidroid 0:ccce492abbb4 55 /* Expansion Board specific header files. */
Davidroid 0:ccce492abbb4 56 #include "XNucleoCCA02M1.h"
Davidroid 0:ccce492abbb4 57
Davidroid 0:ccce492abbb4 58
Davidroid 0:ccce492abbb4 59 /* Variables -----------------------------------------------------------------*/
Davidroid 0:ccce492abbb4 60
Davidroid 0:ccce492abbb4 61 /* Initialization parameters. */
Davidroid 0:ccce492abbb4 62 STA350BW_init_t sound_terminal_init =
Davidroid 0:ccce492abbb4 63 {
Davidroid 0:ccce492abbb4 64 32000, /* Default Sampling Frequency [Hz]. */
Davidroid 0:ccce492abbb4 65 100 /* Default Volume. */
Davidroid 0:ccce492abbb4 66 };
Davidroid 0:ccce492abbb4 67 XNucleoCCA02M1_init_t microphones_init =
Davidroid 0:ccce492abbb4 68 {
Davidroid 0:ccce492abbb4 69 32000, /* Default Sampling Frequency [Hz]. */
Davidroid 0:ccce492abbb4 70 2 /* Default number of channels. */
Davidroid 0:ccce492abbb4 71 };
Davidroid 0:ccce492abbb4 72
Davidroid 0:ccce492abbb4 73 /* Sound Terminal Component. */
Davidroid 0:ccce492abbb4 74 STA350BW *sound_terminal;
Davidroid 0:ccce492abbb4 75
Davidroid 0:ccce492abbb4 76 /* MEMS Microphones Expansion Board. */
Davidroid 0:ccce492abbb4 77 XNucleoCCA02M1 *microphones;
Davidroid 0:ccce492abbb4 78
Davidroid 0:ccce492abbb4 79 /* Thread to manage I2S peripherals. */
Davidroid 0:ccce492abbb4 80 static rtos::Thread i2s_bh_daemon;
Davidroid 0:ccce492abbb4 81
Davidroid 0:ccce492abbb4 82
Davidroid 0:ccce492abbb4 83 /* Functions -----------------------------------------------------------------*/
Davidroid 0:ccce492abbb4 84
Davidroid 0:ccce492abbb4 85 /**
Davidroid 0:ccce492abbb4 86 * @brief Callback to start playing from the sound terminal the PCM buffer
Davidroid 0:ccce492abbb4 87 * filled by the microphones.
Davidroid 0:ccce492abbb4 88 * @param PCM_buffer Buffer containing the PCM data to play.
Davidroid 0:ccce492abbb4 89 * @param PCM_buffer_bytes Size of the buffer.
Davidroid 0:ccce492abbb4 90 * @retval None.
Davidroid 0:ccce492abbb4 91 */
Davidroid 0:ccce492abbb4 92 void StartPlaying(int16_t *PCM_buffer, uint16_t PCM_buffer_bytes)
Davidroid 0:ccce492abbb4 93 {
Davidroid 0:ccce492abbb4 94 static bool first_time = true;
Davidroid 0:ccce492abbb4 95 if (first_time)
Davidroid 0:ccce492abbb4 96 {
Davidroid 0:ccce492abbb4 97 /* Start playing from the sound terminal. */
Davidroid 0:ccce492abbb4 98 sound_terminal->play(PCM_buffer, PCM_buffer_bytes, true);
Davidroid 0:ccce492abbb4 99 first_time = false;
Davidroid 0:ccce492abbb4 100 }
Davidroid 0:ccce492abbb4 101 }
Davidroid 0:ccce492abbb4 102
Davidroid 0:ccce492abbb4 103 /**
Davidroid 0:ccce492abbb4 104 * @brief Entry point function of mbedOS.
Davidroid 0:ccce492abbb4 105 * @param None
Davidroid 0:ccce492abbb4 106 * @retval None
Davidroid 0:ccce492abbb4 107 */
Davidroid 0:ccce492abbb4 108 int main(void)
Davidroid 0:ccce492abbb4 109 {
Davidroid 0:ccce492abbb4 110 /*----- Initialization. -----*/
Davidroid 0:ccce492abbb4 111
Davidroid 0:ccce492abbb4 112 /* Initializing I2C bus. */
Davidroid 0:ccce492abbb4 113 DevI2C *dev_i2c = new DevI2C(PB_9, PB_8);
Davidroid 0:ccce492abbb4 114
Davidroid 0:ccce492abbb4 115 /* Initializing Sound Terminal Component on the I2S2 interface. */
Davidroid 0:ccce492abbb4 116 sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_2, *dev_i2c, PC_12, PC_10, PA_4, NC, PC_7);
Davidroid 0:ccce492abbb4 117 if (sound_terminal->init(&sound_terminal_init) != COMPONENT_OK)
Davidroid 0:ccce492abbb4 118 {
Davidroid 0:ccce492abbb4 119 error("Initialization of the Sound Terminal Expansion Board failed.\r\n");
Davidroid 0:ccce492abbb4 120 exit(EXIT_FAILURE);
Davidroid 0:ccce492abbb4 121 }
Davidroid 0:ccce492abbb4 122
Davidroid 0:ccce492abbb4 123 /* Initializing MEMS Microphones Expansion Board on the I2S1 interface. */
Davidroid 0:ccce492abbb4 124 microphones = new XNucleoCCA02M1(PB_15, PB_13);
Davidroid 0:ccce492abbb4 125 if (microphones->init(&microphones_init) != COMPONENT_OK)
Davidroid 0:ccce492abbb4 126 {
Davidroid 0:ccce492abbb4 127 error("Initialization of the MEMS Microphones Expansion Board failed.\r\n");
Davidroid 0:ccce492abbb4 128 exit(EXIT_FAILURE);
Davidroid 0:ccce492abbb4 129 }
Davidroid 0:ccce492abbb4 130
Davidroid 0:ccce492abbb4 131 /* Asking the I2S peripherals to work at frequencies one multiple of the
Davidroid 0:ccce492abbb4 132 other in order to avoid audio glitches that may be caused by a drift
Davidroid 0:ccce492abbb4 133 between the I2S clock signals.*/
Davidroid 0:ccce492abbb4 134 int ret = I2S::harmonize(sound_terminal->dev_i2s, microphones->dev_i2s);
Davidroid 0:ccce492abbb4 135 if (ret != 0)
Davidroid 0:ccce492abbb4 136 error("Unable to synchronize audio frequencies.\r\n");
Davidroid 0:ccce492abbb4 137
Davidroid 0:ccce492abbb4 138 /* Starting a thread to manage I2S peripherals. */
Davidroid 0:ccce492abbb4 139 Callback<void()> i2s_bh_task(&I2S::i2s_bh_queue, &events::EventQueue::dispatch_forever);
Davidroid 0:ccce492abbb4 140 i2s_bh_daemon.start(i2s_bh_task);
Davidroid 0:ccce492abbb4 141
Davidroid 0:ccce492abbb4 142 /* Printing to the console. */
Davidroid 0:ccce492abbb4 143 printf("Audio Karaoke Application Example\r\n\n");
Davidroid 0:ccce492abbb4 144
Davidroid 0:ccce492abbb4 145
Davidroid 0:ccce492abbb4 146 /*----- Recording and playing. -----*/
Davidroid 0:ccce492abbb4 147
Davidroid 0:ccce492abbb4 148 /* Printing to the console. */
Davidroid 0:ccce492abbb4 149 printf("--> Sing a song and have fun!\r\n");
Davidroid 0:ccce492abbb4 150
Davidroid 0:ccce492abbb4 151 /* Attaching a callback which will be called each time the microphones have
Davidroid 0:ccce492abbb4 152 recorded 1 ms of audio data, and start recording. */
Davidroid 0:ccce492abbb4 153 microphones->attach(&StartPlaying);
Davidroid 0:ccce492abbb4 154 microphones->record();
Davidroid 0:ccce492abbb4 155 }
Davidroid 0:ccce492abbb4 156
Davidroid 0:ccce492abbb4 157 #else // DEVICE_I2S
Davidroid 0:ccce492abbb4 158
Davidroid 0:ccce492abbb4 159 int main(void)
Davidroid 0:ccce492abbb4 160 {
Davidroid 0:ccce492abbb4 161 printf("The target does not support I2S API.\r\n");
Davidroid 0:ccce492abbb4 162 }
Davidroid 0:ccce492abbb4 163
Davidroid 0:ccce492abbb4 164 #endif // DEVICE_I2S