| Kinetis SDK Demo Applications User Guide 1.0.0-beta Freescale Semiconductor, Inc.

- Main Page
- Related Pages
- Demos

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
modulator.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
10 *
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 */
31
32 #ifndef __MODULATOR_H__
33 #define __MODULATOR_H__
34 /*******************************************************************************
35 * Standard C Included Files
36 ******************************************************************************/
37 #include <stdint.h>
38 /*******************************************************************************
39 * SDK Included Files
40 ******************************************************************************/
41 #include "fsl_soundcard.h"
42 #include "fsl_sai_driver.h"
43 #include "fsl_sai_features.h"
44 /*******************************************************************************
45 * Include CMSIS-DSP library
46 ******************************************************************************/
47 #include "arm_math.h"
48 /*******************************************************************************
49 * Definitions
50 ******************************************************************************/
51 /* @brief Standard Winodws PCM wave file header length */
52 #define WAVE_FILE_HEADER_SIZE 0x2CU
53 #define WAV_PARTIAL_BUFFER_SIZE 512
54 #define WAV_PI 3.14159265
55 /*******************************************************************************
56 * Constants
57 ******************************************************************************/
58 /* Modulations available */
59 enum wav_mods
60 {
61 kNoModulation,
62 kSquareWave,
63 kSawWave,
64 kTriangleWave
65 };
66 /*******************************************************************************
67 * Data Structures
68 ******************************************************************************/
69 /* @brief Standard Windows PCM wave file header struct. */
70 typedef struct wave_header
71 {
72 uint8_t riff[4];
73 uint32_t size;
74 uint8_t waveFlag[4];
75 uint8_t fmt[4];
76 uint32_t fmtLen;
77 uint16_t tag;
78 uint16_t channels;
79 uint32_t sampFreq;
80 uint32_t byteRate;
81 uint16_t blockAlign;
82 uint16_t bitSamp;
83 uint8_t dataFlag[4];
84 uint32_t length;
85 } wave_header_t;
86
87 /* @brief Wave file struct */
88 typedef struct wave_file
89 {
90 wave_header_t header;
91 uint32_t *data;
92 } wave_file_t;
93
94 /*******************************************************************************
95 * Prototypes
96 ******************************************************************************/
97
98 /*
99 * @brief Initialize I2S, I2C, & TWR-AUDIO-SGTL board.
100 *
101 */
102 void modulator_init(void);
103
104 /*
105 * @brief Collects data from wav file header.
106 *
107 * @param waveFile Data structure of pcm data array.
108 *
109 * @return status_t Return kStatus_Success if function completed successfully, return kStatusFail if function failed.
110 *
111 */
112 snd_status_t get_wav_data(wave_file_t *waveFile);
113
114 /*
115 * @brief Play PCM audio data from wav format array.
116 *
117 * @param pcmBuffer Pointer to data array containing wav formatted audio data.
118 *
119 * @param divider Integer to divide PCM data length to adjust audio sample duration.
120 *
121 * @return status_t Return kStatus_Success if function completed successfully, return kStatusFail if function failed.
122 *
123 */
124 snd_status_t play_wav(uint32_t *pcmBuffer, uint8_t divider);
125
126 /*
127 * @brief Send audio data to sound card.
128 *
129 * @param dataBuffer Pointer to data array containing wav formatted audio data.
130 *
131 * @param length Length of data to send to sound card.
132 *
133 * @param dataFormat Point to audio_data_format_t for sound card.
134 *
135 */
136 static void send_wav(uint8_t *dataBuffer, uint32_t length, sai_data_format_t *dataFormat);
137
138 /*
139 * @brief Play modulated PCM audio data from wav format array.
140 *
141 * @param pcmBuffer Pointer to data array containing wav formatted audio data.
142 *
143 * @param modBuffer Pointer to data array to store modulated PCM data.
144 *
145 * @param fftData Pointer to data array for storing Fast Fourier Transform data.
146 *
147 * @param fftResult Point to data array for storing real frequency bins from FFT.
148 *
149 * @param divider Integer to divide PCM data length to adjust audio sample duration.
150 *
151 * @param modulation Constant representing type of waveform acheive from modulation.
152 *
153 * @param srcSizeBytes Size of wav format array being processed.
154 *
155 * @param sampleSize Size of sample for FFT (each sample is 16-bit integer from PCM data).
156 *
157 * @return status_t Return kStatus_Success if function completed successfully, return kStatusFail if function failed.
158 *
159 */
160 snd_status_t play_mod_wav(uint16_t *pcmBuffer, uint16_t *modPointer, float32_t *fftData, float32_t *fftResult, uint8_t divider, uint8_t modulation, uint32_t srcSizeBytes, uint32_t sampleSize);
161
162 /*
163 * @brief Find fundamental frequency of PCM data in wav format array.
164 *
165 * @param pcmBuffer Pointer to data array containing wav formatted audio data.
166 *
167 * @param fftData Pointer to data array for storing Fast Fourier Transform data.
168 *
169 * @param fftResult Point to data array for storing real frequency bins from FFT.
170 *
171 * @param sampleSize Size of sample for FFT (each sample is 16-bit integer from PCM data).
172 *
173 * @return float32_t Returns fundamental frequency in Hz.
174 *
175 */
176 float32_t get_wav_Hz(uint16_t *pcmBuffer, float32_t *fftData, float32_t *fftResult, uint32_t sampleSize);
177
178 /*
179 * @brief Find fundamental frequency of PCM data in wav format array.
180 *
181 * @param pcmBuffer Pointer to data array containing wav formatted audio data.
182 *
183 * @param modBuffer Pointer to data array to store modulated PCM data.
184 *
185 * @param fftData Pointer to data array for storing Fast Fourier Transform data.
186 *
187 * @param fftResult Point to data array for storing real frequency bins from FFT.
188 *
189 * @param startIndex Starting index of array to get PCM data from.
190 *
191 * @param sampleSize Size of sample for FFT (each sample is 16-bit integer from PCM data).
192 *
193 * @param modType Constant representing type of waveform acheive from modulation.
194 *
195 * @return float32_t Returns fundamental frequency in Hz.
196 *
197 */
198 uint32_t mod_wav_data(uint16_t *pcmBuffer, uint16_t *modBuffer, float32_t *fftData, float32_t *fftResult, uint16_t startIndex, uint32_t sampleSize, uint8_t modType);
199
200 /*
201 * @brief Play modulated PCM audio data from array of wav format data arrays.
202 *
203 * @param arpeggioBuffer Pointer to data array containing wav formatted audio data.
204 *
205 * @param introLength The number of notes in the arpeggio.
206 *
207 * @param divider Integer to divide PCM data length to adjust audio sample duration.
208 *
209 */
210 void play_wav_arpeggio(uint32_t *arpeggioBuffer, uint32_t introLength, uint8_t divider);
211
212 #endif /* __MODULATOR_H__ */
213
214 /*******************************************************************************
215 * EOF
216 *******************************************************************************/
kTriangleWave
Definition: modulator.h:64
get_wav_data
snd_status_t get_wav_data(wave_file_t *waveFile)
Definition: modulator.c:104
modBuffer
unsigned char modBuffer[sizeof(sin261Hz)]
Definition: pcm_data.h:11622
wave_header_t::sampFreq
uint32_t sampFreq
Definition: modulator.h:79
wave_file_t::data
uint32_t * data
Definition: modulator.h:91
wave_header_t::fmtLen
uint32_t fmtLen
Definition: modulator.h:76
wav_mods
wav_mods
Definition: modulator.h:59
wave_header_t::tag
uint16_t tag
Definition: modulator.h:77
dataBuffer
const uint16_t dataBuffer[]
Definition: data_file.h:50
wave_file_t::header
wave_header_t header
windows PCM wavfile header
Definition: modulator.h:90
wave_header_t::size
uint32_t size
Definition: modulator.h:73
wave_header_t::byteRate
uint32_t byteRate
Definition: modulator.h:80
play_wav_arpeggio
void play_wav_arpeggio(uint32_t *arpeggioBuffer, uint32_t length, uint8_t divider)
Definition: modulator.c:514
wave_header_t::bitSamp
uint16_t bitSamp
Definition: modulator.h:82
send_wav
static void send_wav(uint8_t *dataBuffer, uint32_t length, sai_data_format_t *dataFormat)
wave_header_t::blockAlign
uint16_t blockAlign
Definition: modulator.h:81
kSquareWave
Definition: modulator.h:62
play_mod_wav
snd_status_t play_mod_wav(uint16_t *pcmBuffer, uint16_t *modPointer, float32_t *fftData, float32_t *fftResult, uint8_t divider, uint8_t modulation, uint32_t srcSizeBytes, uint32_t sampleSize)
Definition: modulator.c:256
get_wav_Hz
float32_t get_wav_Hz(uint16_t *pcmBuffer, float32_t *fftData, float32_t *fftResult, uint32_t sampleSize)
Definition: modulator.c:283
play_wav
snd_status_t play_wav(uint32_t *pcmBuffer, uint8_t divider)
Definition: modulator.c:173
modulator_init
void modulator_init(void)
Definition: modulator.c:66
wave_header_t::length
uint32_t length
Definition: modulator.h:84
kSawWave
Definition: modulator.h:63
wave_header_t
Definition: modulator.h:70
mod_wav_data
uint32_t mod_wav_data(uint16_t *pcmBuffer, uint16_t *modBuffer, float32_t *fftData, float32_t *fftResult, uint16_t startIndex, uint32_t sampleSize, uint8_t modType)
Definition: modulator.c:357
kNoModulation
Definition: modulator.h:61
wave_file_t
Definition: modulator.h:88
wave_header_t::channels
uint16_t channels
Definition: modulator.h:78