Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: LCD_fonts SPI_TFT_ILI9341 CMSIS_DSP_401_without_cm4 mbed-src SDFileSystem wavfile
audio_if.cpp
- Committer:
- linx0576
- Date:
- 2015-12-13
- Revision:
- 10:0986108f8aa3
- Parent:
- 9:5a860b9c8a6a
File content as of revision 10:0986108f8aa3:
/**
******************************************************************************
* @file /Src/audio_if.c
* @author Central Labs
* @version V1.0.0
* @date 7-May-2015
* @brief USB Device Audio interface file.
******************************************************************************
@attention
*
* <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (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.st.com/software_license_agreement_liberty_v2
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "audio_if.h"
//uint16_t PDM_Buffer[AUDIO_CHANNELS*AUDIO_SAMPLING_FREQUENCY/1000*64/8];
//static uint16_t PCM_Buffer[AUDIO_CHANNELS*AUDIO_SAMPLING_FREQUENCY/1000];
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* This table maps the audio device class setting in 1/256 dB to a
* linear 0-64 scaling used in pdm_filter.c. It is computed as
* 256*20*log10(index/64). */
//const int16_t vol_table[65] =
//{ 0x8000, 0xDBE0, 0xE1E6, 0xE56B, 0xE7EB, 0xE9DB, 0xEB70, 0xECC7,
//0xEDF0, 0xEEF6, 0xEFE0, 0xF0B4, 0xF176, 0xF228, 0xF2CD, 0xF366,
//0xF3F5, 0xF47C, 0xF4FB, 0xF574, 0xF5E6, 0xF652, 0xF6BA, 0xF71C,
//0xF778, 0xF7D6, 0xF82D, 0xF881, 0xF8D2, 0xF920, 0xF96B, 0xF9B4,
//0xF9FB, 0xFA3F, 0xFA82, 0xFAC2, 0xFB01, 0xFB3E, 0xFB79, 0xFBB3,
//0xFBEB, 0xFC22, 0xFC57, 0xFC8C, 0xFCBF, 0xFCF1, 0xFD22, 0xFD51,
//0xFD80, 0xFDAE, 0xFDDB, 0xFE07, 0xFE32, 0xFE5D, 0xFE86, 0xFEAF,
//0xFED7, 0xFF00, 0xFF25, 0xFF4B, 0xFF70, 0xFF95, 0xFFB9, 0xFFD0,
//0x0000 };
//volatile uint8_t VolumeSetting=64;
/**
* @brief Initializes the AUDIO media low layer.
* @param AudioFreq: Audio frequency used to play the audio stream.
* @param BitRes: desired bit resolution
* @param ChnlNbr: number of channel to be configured
* @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise
*/
int8_t Audio_Init(void)
{
return BSP_AUDIO_IN_Init(48000, 16, 1);
}
/**
* @brief Start audio recording engine
* @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise
*/
int8_t Audio_Record(uint16_t * recbuff)
{
int8_t res = BSP_AUDIO_IN_Record(recbuff, 2048);
// AudioProcess(pbuf, recbuff);
return res;
}
/**
* @brief Controls AUDIO Volume.
* @param vol: Volume level
* @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise
*/
//int8_t Audio_VolumeCtl(int16_t Volume)
//{
// /* Call low layer volume setting function */
// int j;
//
// j = 0;
// /* Find the setting nearest to the desired setting */
// while(j<64 &&
// abs(Volume-vol_table[j]) > abs(Volume-vol_table[j+1])) {
// j++;
// }
// /* Now do the volume adjustment */
// VolumeSetting = (uint8_t)j;
// return BSP_AUDIO_IN_SetVolume(VolumeSetting);
//
//
//}
/**
* @brief Controls AUDIO Mute.
* @param cmd: Command opcode
* @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise
*/
//int8_t Audio_MuteCtl(uint8_t cmd)
//{
// return AUDIO_OK;
//}
/**
* @brief Stops audio acquisition
* @param none
* @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise
*/
int8_t Audio_Stop(void)
{
return BSP_AUDIO_IN_Stop();
}
/**
* @brief Pauses audio acquisition
* @param none
* @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise
*/
//
//int8_t Audio_Pause(void)
//{
// return 0;
//}
/**
* @brief Resumes audio acquisition
* @param none
* @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise
*/
//int8_t Audio_Resume(void)
//{
// return AUDIO_OK;
//}
/**
* @brief Manages command from usb
* @param None
* @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise
*/
//
//int8_t Audio_CommandMgr(uint8_t cmd)
//{
// return AUDIO_OK;
//}
//
//void AudioProcess(uint16_t * pbuf, uint16_t * recbuff)
//{
// BSP_AUDIO_IN_PDMToPCM(pbuf,recbuff);
// // need to copy recbuff to final output buff (in chunks)
//}
void PrintData(uint16_t * recbuff)
{
for (int i=0; i < AUDIO_CHANNELS*AUDIO_SAMPLING_FREQUENCY/1000; ++i) {
if (!(i%10))
pc.printf("\r\n\t");
pc.printf("%d ", recbuff[i]);
}
pc.printf("\r\n\r\n");
}
void WriteData(uint16_t * recbuff)
{
//Try to mount the SD card
pc.printf("Mounting SD card...");
if (sd.mount() != 0) {
pc.printf("failed!\r\n");
return;
}
pc.printf("mounted successfully!\r\n");
// write event to log
FILE *fp = fopen("/sd/event_log.txt", "a");
if (fp != NULL) {
for (int i=0; i < AUDIO_CHANNELS*AUDIO_SAMPLING_FREQUENCY/1000; ++i) {
if (!(i%10))
fprintf(fp, "\r\n");
fprintf(fp, "%d ", recbuff[i]);
}
fprintf(fp, "\r\n\r\n");
fprintf(fp, "Woohoo! I made a file!\r\n");
fclose(fp);
PrintData(recbuff);
pc.printf("success!\r\n");
} else {
pc.printf("failed!\r\n");
}
pc.printf("creating WAV file");
static const char *target_filename = "/sd/rec-test.wav";
rec(target_filename, recbuff, 48);
sd.unmount();
pc.printf("sd card unmounted\r\n\r\n");
}
#define WAVFILE_ERROR_PRINT(RESULT) \
do { \
WavFileResult R = RESULT; \
if (R != WavFileResultOK) { \
char wavfile_error_print_text[BUFSIZ]; \
wavfile_result_string(R, wavfile_error_print_text, sizeof(wavfile_error_print_text)); \
printf("%s (code=%d)\r\n", wavfile_error_print_text, R); \
return 1; \
} \
} while(0)
int rec(const char *filename, uint16_t * recbuff, uint16_t size)
{
WavFileResult result;
wavfile_info_t info;
wavfile_data_t data;
WAVFILE_INFO_AUDIO_FORMAT(&info) = 1;
WAVFILE_INFO_NUM_CHANNELS(&info) = 1;
WAVFILE_INFO_SAMPLE_RATE(&info) = 1000;
WAVFILE_INFO_BYTE_RATE(&info) = 2000;
WAVFILE_INFO_BLOCK_ALIGN(&info) = 2;
WAVFILE_INFO_BITS_PER_SAMPLE(&info) = 16;
WAVFILE *wf = wavfile_open(filename, WavFileModeWrite, &result);
WAVFILE_ERROR_PRINT(result);
WAVFILE_ERROR_PRINT(wavfile_write_info(wf, &info));
pc.printf("[REC:%s]\r\n", filename);
pc.printf("\tWAVFILE_INFO_AUDIO_FORMAT(&info) = %d\r\n", WAVFILE_INFO_AUDIO_FORMAT(&info));
pc.printf("\tWAVFILE_INFO_NUM_CHANNELS(&info) = %d\r\n", WAVFILE_INFO_NUM_CHANNELS(&info));
pc.printf("\tWAVFILE_INFO_SAMPLE_RATE(&info) = %d\r\n", WAVFILE_INFO_SAMPLE_RATE(&info));
pc.printf("\tWAVFILE_INFO_BYTE_RATE(&info) = %d\r\n", WAVFILE_INFO_BYTE_RATE(&info));
pc.printf("\tWAVFILE_INFO_BLOCK_ALIGN(&info) = %d\r\n", WAVFILE_INFO_BLOCK_ALIGN(&info));
pc.printf("\tWAVFILE_INFO_BITS_PER_SAMPLE(&info) = %d\r\n", WAVFILE_INFO_BITS_PER_SAMPLE(&info));
// const int interval_us = 1000000 / WAVFILE_INFO_SAMPLE_RATE(&info);
int dropout = 0;
// unsigned int count = 0;
WAVFILE_DATA_NUM_CHANNELS(&data) = 1;
// for (int i=0; i<size; ++i) {
// WAVFILE_DATA_CHANNEL_DATA(&data, 0) = 0; //buffer[rp];
// WAVFILE_ERROR_PRINT(wavfile_write_data(wf, &data));
WAVFILE_ERROR_PRINT(wavfile_write_data_bulk_uint16(wf, recbuff, AUDIO_CHANNELS, size));
// }
pc.printf("\t-- Rec done. (dropout=%d) --\r\n", dropout);
WAVFILE_ERROR_PRINT(wavfile_close(wf));
return 0;
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/