ARMEBS4  revision-26.06.2015
audio.h
Go to the documentation of this file.
1 /************************************************************************//**
2  * \file heivs/audio.h
3  *
4  * \brief ARMEBS4 audio
5  *
6  * \author marc dot pignat at hevs dot ch & Pascal Sartoretti (sap at hevs dot ch)
7  * \addtogroup libheivs_stm32_audio Audio
8  * \ingroup libheivs_stm32
9  * @{
10  *
11  * == Full-duplex notes ==
12  * Full duplex is possible usint Audio_Stream_* function.
13  * * Audio_Stream_Play_Init must be called before Audio_Stream_Record_Init
14  * * Audio_Stream_Stop will "pause" audio recording, but Audio_Stream_Pause can
15  * be used.
16  * \see trunk/demos/audio_stream_demo
17  *
18  ***************************************************************************/
19 
20 #ifndef HEIVS_AUDIO_H
21 #define HEIVS_AUDIO_H
22 
23 #include "heivs/error.h"
24 
25 #ifdef __cplusplus
26  extern "C" {
27 #endif
28 
29 /************************************************************************//**
30  * \brief Audio initialization
31  *
32  * \param fs the sampling frequency (96000,48000,44100,32000,16000,8000)
33  * \param sample_size (32,24 or 16 bit)
34  *
35  * \return NO_ERROR for no problem
36  ***************************************************************************/
37 status_e Audio_Init(uint32_t fs, uint32_t sample_size);
38 
39 /************************************************************************//**
40  * \brief Play a sine sound at a given frequency.
41  *
42  * \param freq Frequency of sine. \see note_e
43  * \param duration duration in milliseconds
44  *
45  * \return NO_ERROR for no problem
46  ***************************************************************************/
47 status_e Audio_PlaySin(uint16_t freq, uint16_t duration);
48 
49 /************************************************************************//**
50  * \brief Set the volume for speaker output
51  *
52  * \param volume Volume to set (0 is mute, 255 is max)
53  *
54  * \return NO_ERROR for no problem
55  ***************************************************************************/
56 status_e Audio_SetVolumeSpeaker(uint8_t volume);
57 
58 /************************************************************************//**
59  * \brief Set the volume for headphone output
60  *
61  * \param volume Volume to set (0 is mute, 255 is max) *
62  * \return NO_ERROR for no problem
63  ***************************************************************************/
64 status_e Audio_SetVolumeHeadphone(uint8_t volume);
65 
66 /************************************************************************//**
67  * \brief Record a sound (polling mode)
68  *
69  * \param dataBuf Data buffer of sound to record
70  * \param length byte length of the buffer
71  *
72  * \return NO_ERROR for no problem
73  ***************************************************************************/
74 status_e Audio_Record(int16_t * dataBuf, uint32_t length);
75 
76 /************************************************************************//**
77  * \brief Play a sound (polling mode)
78  *
79  * \param dataBuf Data buffer of sound to record
80  * \param length byte length of the buffer
81  *
82  * \return NO_ERROR for no problem
83  ***************************************************************************/
84 status_e Audio_Play(const int16_t * dataBuf, uint32_t length);
85 
86 /************************************************************************//**
87  * \brief Play a sound (DMA version)
88  *
89  * \param data the buffer
90  * \param length byte length of the buffer
91  * \param done_handler Function to be called when the sound has finished (will be called from interrupt context), can be NULL.
92  *
93  * \return NO_ERROR for no problem, ERROR_AGAIN when busy, another error if any
94  *
95  * \see Audio_DMA_Play_Status
96  ***************************************************************************/
97 status_e Audio_DMA_Play(const void *data, uint32_t length, void (*done)(const void *));
98 
99 /************************************************************************//**
100  * \brief Is Audio_DMA_Play finished?
101  *
102  * \return ERROR_AGAIN if the sound has not finished playing, NO_ERROR when finished
103  *
104  * \see Audio_DMA_Play
105  ***************************************************************************/
107 
108 /************************************************************************//**
109  * \brief Audio stream play initialization
110  *
111  * \param length byte length of each buffer
112  * \param buffer0 the first buffer to be played
113  * \param buffer1 the second buffer to be played
114  * \param done the handler called after a buffer has been played. Called in interrupt context. can be NULL if not used
115  *
116  * \return NO_ERROR for no problem
117  *
118  * \warning buffer0 and buffer1 are expected to exist until \see Audio_Stream_Play_Stop
119  *
120  * The DMA will play alternatively buffer0 and buffer1. \see Audio_Stream_Play
121  * will be used to replace the next buffer.
122  *
123  * The stream will be *Paused* after this function
124  *
125  ***************************************************************************/
126 status_e Audio_Stream_Play_Init(uint32_t length, void *buffer0, void *buffer1, void (*done)(const void *));
127 
128 /************************************************************************//**
129  * \brief Audio stream play buffer
130  * \param data buffer to be played
131  * \return NO_ERROR for no problem
132  *
133  * \see Audio_Stream_Status
134  *
135  * \warning This function will block unless Audio_Stream_Play_Status has returned NO_ERROR
136  ***************************************************************************/
137 status_e Audio_Stream_Play(const void *data);
138 
139 /************************************************************************//**
140  * \brief Audio stream play pause
141  *
142  * \return NO_ERROR for no problem
143  *
144  * \see Audio_Stream_Play for (re-)starting playback
145  *
146  * This function will in fact fill the output buffers with zeroes, effectively
147  * muting the output.
148  *
149  ***************************************************************************/
151 
152 /************************************************************************//**
153  * \brief Audio stream playing stop
154  *
155  * \return NO_ERROR for no problem
156  ***************************************************************************/
158 
159 /************************************************************************//**
160  * \brief Is Audio_Stream_Play ready for the next buffer?
161  *
162  * \return ERROR_AGAIN if not ready, NO_ERROR when ready
163  *
164  * \see Audio_DMA_Play
165  ***************************************************************************/
167 
168 /************************************************************************//**
169  * \brief Audio stream record initialization
170  *
171  * \param length byte length of each buffer
172  * \param buffer0 the first buffer to be played
173  * \param buffer1 the second buffer to be played
174  * \param done handler called with the data recorded. Called in interrupt context
175  *
176  * \return NO_ERROR for no problem
177  *
178  * \warning buffer0 and buffer1 are expected to exist until \see Audio_Stream_Record_Stop
179  *
180  * The DMA will record alternatively into buffer0 and buffer1.
181  ***************************************************************************/
182 status_e Audio_Stream_Record_Init(uint32_t length, void *buffer0, void *buffer1, void(*done)(const void *));
183 
184 /************************************************************************//**
185  * \brief Audio stream record stop
186  * \return NO_ERROR for no problem
187  ***************************************************************************/
189 
190 /**
191  * Music notes
192  */
193 enum note_e
194 {
195  DO = 262,
196  DOd = 277,
197  RE = 294,
198  MIb = 311,
199  MI = 330,
200  FA = 349,
201  FAd = 370,
202  SOL = 392,
203  SOLd = 415,
204  LA = 440,
205  SIb = 466,
206  SI = 494,
207 };
208 
209 #ifdef __cplusplus
210  }
211 #endif
212 
213 /**
214  * @}
215  */
216 #endif /* HEIVS_AUDIO_H */
status_e Audio_DMA_Play_Status(void)
Is Audio_DMA_Play finished?
Definition: audio_dma.c:60
status_e Audio_Stream_Play_Pause(void)
Audio stream play pause.
Definition: audio.c:112
status_e Audio_DMA_Play(const void *data, uint32_t length, void(*done)(const void *))
Play a sound (DMA version)
Definition: audio_dma.c:45
status_e Audio_Stream_Play(const void *data)
Audio stream play buffer.
Definition: audio.c:117
status_e Audio_SetVolumeSpeaker(uint8_t volume)
Set the volume for speaker output.
Definition: audio.c:240
Errors definitions.
status_e Audio_Stream_Play_Stop(void)
Audio stream playing stop.
Definition: audio.c:102
status_e Audio_Stream_Record_Init(uint32_t length, void *buffer0, void *buffer1, void(*done)(const void *))
Audio stream record initialization.
Definition: audio.c:122
status_e Audio_Stream_Record_Stop(void)
Audio stream record stop.
Definition: audio.c:127
status_e Audio_Stream_Play_Init(uint32_t length, void *buffer0, void *buffer1, void(*done)(const void *))
Audio stream play initialization.
Definition: audio.c:97
status_e Audio_Record(int16_t *dataBuf, uint32_t length)
Record a sound (polling mode)
Definition: audio.c:235
note_e
Definition: audio.h:193
status_e Audio_Stream_Play_Status(void)
Is Audio_Stream_Play ready for the next buffer?
Definition: audio.c:107
status_e Audio_SetVolumeHeadphone(uint8_t volume)
Set the volume for headphone output.
Definition: audio.c:245
status_e
Known errors.
Definition: error.h:21
status_e Audio_Init(uint32_t fs, uint32_t sample_size)
Audio initialization.
Definition: audio.c:140
status_e Audio_PlaySin(uint16_t freq, uint16_t duration)
Play a sine sound at a given frequency.
Definition: audio.c:184
status_e Audio_Play(const int16_t *dataBuf, uint32_t length)
Play a sound (polling mode)
Definition: audio.c:230