µGFX  2.9
version 2.9
gaudio.h
Go to the documentation of this file.
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.io/license.html
6  */
7 
8 /**
9  * @file src/gaudio/gaudio.h
10  *
11  * @addtogroup GAUDIO
12  *
13  * @brief Module to handle audio recording and play-back
14  *
15  * @{
16  */
17 
18 #ifndef _GAUDIO_H
19 #define _GAUDIO_H
20 
21 #include "../../gfx.h"
22 
23 #if GFX_USE_GAUDIO || defined(__DOXYGEN__)
24 
25 /* Include the driver defines */
26 #if GAUDIO_NEED_PLAY
27  #include "gaudio_play_config.h"
28 #endif
29 #if GAUDIO_NEED_RECORD
30  #include "gaudio_record_config.h"
31 #endif
32 
33 /*===========================================================================*/
34 /* Type definitions */
35 /*===========================================================================*/
36 
37 // Event types for GAUDIO
38 #define GEVENT_AUDIO_PLAY (GEVENT_GAUDIO_FIRST+0)
39 #define GEVENT_AUDIO_RECORD (GEVENT_GAUDIO_FIRST+1)
40 
41 #if GFX_USE_GEVENT || defined(__DOXYGEN__)
42  /**
43  * @brief The Audio play event structure.
44  * @{
45  */
46  typedef struct GEventAudioPlay_t {
47  /**
48  * @brief The type of this event (GEVENT_AUDIO_PLAY)
49  */
50  GEventType type;
51  /**
52  * @brief The event flags
53  */
54  gU16 flags;
55  /**
56  * @brief The event flag values.
57  * @{
58  */
59  #define GAUDIO_PLAY_LOSTEVENT 0x0001 /**< @brief The last GEVENT_AUDIO_PLAY event was lost */
60  #define GAUDIO_PLAY_PLAYING 0x0002 /**< @brief The audio out system is currently playing */
61  #define GAUDIO_PLAY_FREEBLOCK 0x0004 /**< @brief An audio buffer has been freed */
62  /** @} */
64  /** @} */
65 
66  /**
67  * @brief The Audio record event structure.
68  * @{
69  */
70  typedef struct GEventAudioRecord_t {
71  /**
72  * @brief The type of this event (GEVENT_AUDIO_RECORD)
73  */
74  GEventType type;
75  /**
76  * @brief The event flags
77  */
78  gU16 flags;
79  /**
80  * @brief The event flag values.
81  * @{
82  */
83  #define GAUDIO_RECORD_LOSTEVENT 0x0001 /**< @brief The last GEVENT_AUDIO_IN event was lost */
84  #define GAUDIO_RECORD_RECORDING 0x0002 /**< @brief The audio recording system is currently recording */
85  #define GAUDIO_RECORD_GOTBUFFER 0x0004 /**< @brief An audio buffer is ready for processing */
86  #define GAUDIO_RECORD_STALL 0x0008 /**< @brief The recording process has stalled due to no free buffers */
87  /** @} */
89  /** @} */
90 #endif
91 
92 /*===========================================================================*/
93 /* External declarations. */
94 /*===========================================================================*/
95 
96 #if GAUDIO_NEED_PLAY || defined(__DOXYGEN__)
97  /**
98  * @brief Set the audio device to play on the specified channel and with the specified
99  * sample frequency.
100  * @return gTrue is successful, gFalse if the driver doesn't accept those parameters.
101  *
102  * @param[in] channel The audio output channel to use. Can be set from 0 to GAUDIO_PLAY_NUM_CHANNELS - 1
103  * @param[in] frequency The audio sample rate in samples per second
104  * @param[in] format The audio sample format
105  *
106  * @note Some channels are mono, and some are stereo. See your driver config file
107  * to determine which channels to use and whether they are stereo or not.
108  * @note Only one channel can be playing at a time. Calling this will stop any
109  * currently playing channel.
110  *
111  * @api
112  */
113  gBool gaudioPlayInit(gU16 channel, gU32 frequency, ArrayDataFormat format);
114 
115  /**
116  * @brief Play the specified sample data.
117  * @details The sample data is output to the audio channel. On completion the buffer is returned to the free-list.
118  * @pre @p gaudioPlayInit must have been called first to set the channel and sample frequency.
119  *
120  * @param[in] paud The audio sample buffer to play. It can be NULL (used to restart paused audio)
121  *
122  * @note Calling this will cancel any pause.
123  * @note Before calling this function the len field of the GDataBuffer structure must be
124  * specified (in bytes).
125  * @note For stereo channels the sample data is interleaved in the buffer.
126  * @note This call returns before the data has completed playing. Subject to available buffers (which
127  * can be obtained from the free-list), any number of buffers may be played. They will be queued
128  * for playing in the order they are supplied to this routine and played when previous buffers are
129  * complete. In this way continuous playing can be obtained without audio gaps.
130  *
131  * @api
132  */
133  void gaudioPlay(GDataBuffer *paud);
134 
135  /**
136  * @brief Pause any currently playing sounds.
137  *
138  * @note If nothing is currently playing this routine does nothing. To restart playing call @p gaudioPlay()
139  * with or without a new sample buffer.
140  * @note Some drivers will not respond until a buffer boundary.
141  *
142  * @api
143  */
144  void gaudioPlayPause(void);
145 
146  /**
147  * @brief Stop any currently playing sounds.
148  *
149  * @note This stops any playing sounds and returns any currently queued buffers back to the free-list.
150  * @note Some drivers will not respond until a buffer boundary.
151  *
152  * @api
153  */
154  void gaudioPlayStop(void);
155 
156  /**
157  * @brief Set the output volume.
158  * @return gTrue if successful.
159  *
160  * @param[in] vol 0->255 (0 = muted)
161  *
162  * @note Some drivers may not support this. They will return gFalse.
163  * @note For stereo devices, both channels are set to the same volume.
164  *
165  * @api
166  */
167  gBool gaudioPlaySetVolume(gU8 vol);
168 
169  #if GFX_USE_GEVENT || defined(__DOXYGEN__)
170  /**
171  * @brief Turn on sending results to the GEVENT sub-system.
172  * @details Returns a GSourceHandle to listen for GEVENT_AUDIO_OUT events.
173  *
174  * @note The audio output will not use the GEVENT system unless this is
175  * called first. This saves processing time if the application does
176  * not want to use the GEVENT sub-system for audio output.
177  * Once turned on it can only be turned off by calling @p gaudioPlayInit() again.
178  * @note The audio output is capable of signaling via this method and other methods
179  * at the same time.
180  *
181  * @return The GSourceHandle
182  *
183  * @api
184  */
185  GSourceHandle gaudioPlayGetSource(void);
186  #endif
187 
188  /**
189  * @brief Wait for any currently playing sounds to complete
190  * @return gTrue if there is now nothing playing or gFalse if the timeout is exceeded
191  *
192  * @param[in] ms The maximum amount of time in milliseconds to wait for playing to complete.
193  *
194  * @api
195  */
196  gBool gaudioPlayWait(gDelay ms);
197 #endif
198 
199 #if GAUDIO_NEED_RECORD || defined(__DOXYGEN__)
200  /**
201  * @brief Initialise (but not start) the Audio Recording sub-system.
202  * @details Returns gFalse for an invalid channel or other invalid parameter.
203  *
204  * @param[in] channel The channel to convert. Can be set from 0 to GAUDIO_RECORD_NUM_CHANNELS - 1
205  * @param[in] frequency The sample frequency
206  * @param[in] format The audio sample format requested
207  *
208  * @note Only one channel is active at a time. If an audio input is running it will be stopped.
209  * The Event subsystem is disconnected from the audio subsystem and any binary semaphore
210  * event is forgotten.
211  * @note Some channels may be stereo channels which return twice as much sample data with
212  * the left and right channel data interleaved. Other channels may be mono channels.
213  * Where stereo channels exist the low level driver may also
214  * offer the left and right channels separately.
215  * @note Due to a bug in Chibi-OS each buffer on the free-list must contain an even number of
216  * samples and for stereo devices it must hold a number of samples that is evenly divisible by 4.
217  * This requirement applies only to ChibiOS where the audio driver uses
218  * a ChibiOS hal driver like the cpu ADC driver. This applies even it is used indirectly via
219  * the uGFX GADC driver.
220  * @note The number of samples for stereo devices will be double the number of conversions.
221  * Make sure you allocate your buffers large enough. Each channel is then interleaved
222  * into the provided buffer.
223  *
224  * @return gFalse if invalid channel or parameter
225  *
226  * @api
227  */
228  gBool gaudioRecordInit(gU16 channel, gU32 frequency, ArrayDataFormat format);
229 
230  /**
231  * @brief Start the audio recording.
232  * @pre It must have been initialised first with @p gaudioRecordInit()
233  *
234  * @api
235  */
236  void gaudioRecordStart(void);
237 
238  /**
239  * @brief Stop the audio recording.
240  *
241  * @note All audio recording data that has not yet been retrieved is automatically
242  * returned to the free-list.
243  * @api
244  */
245  void gaudioRecordStop(void);
246 
247  /**
248  * @brief Get a filled audio buffer from the recording list
249  * @return A GDataBuffer pointer or NULL if the timeout is exceeded
250  *
251  * @param[in] ms The maximum amount of time in milliseconds to wait for data if some is not currently available.
252  *
253  * @note After processing the audio data, your application must return the buffer to the free-list so that
254  * it can be used to record more audio into. This can be done via the play list using @p gaudioPlay() or
255  * directly using @p gfxBufferRelease().
256  * @note A buffer may be returned to the free-list before you have finished processing it provided you finish
257  * processing it before GADC re-uses it. This is useful when RAM usage is critical to reduce the number
258  * of buffers required. It works before the free list is a FIFO queue and therefore buffers are kept
259  * in the queue as long as possible before they are re-used.
260  *
261  * @api
262  */
264 
265  #if GFX_USE_GEVENT || defined(__DOXYGEN__)
266  /**
267  * @brief Turn on sending results to the GEVENT sub-system.
268  * @details Returns a GSourceHandle to listen for GEVENT_AUDIO_RECORD events.
269  *
270  * @note Audio recording will not use the GEVENT system unless this is
271  * called first. This saves processing time if the application does
272  * not want to use the GEVENT sub-system for audio recording.
273  * Once turned on it can only be turned off by calling @p gaudioRecordInit() again.
274  * @note The audio input is capable of signaling via this and other methods
275  * at the same time.
276  *
277  * @return The GSourceHandle
278  *
279  * @api
280  */
281  GSourceHandle gaudioRecordGetSource(void);
282  #endif
283 #endif
284 
285 #endif /* GFX_USE_GAUDIO */
286 
287 #endif /* _GAUDIO_H */
288 /** @} */
289 
void gaudioPlayStop(void)
Stop any currently playing sounds.
GSourceHandle gaudioPlayGetSource(void)
Turn on sending results to the GEVENT sub-system.
void gaudioRecordStart(void)
Start the audio recording.
struct GEventAudioRecord_t GEventAudioRecord
The Audio record event structure.
gBool gaudioPlaySetVolume(gU8 vol)
Set the output volume.
GDataBuffer * gaudioRecordGetData(gDelay ms)
Get a filled audio buffer from the recording list.
void gaudioRecordStop(void)
Stop the audio recording.
gBool gaudioPlayInit(gU16 channel, gU32 frequency, ArrayDataFormat format)
Set the audio device to play on the specified channel and with the specified sample frequency.
struct GEventAudioPlay_t GEventAudioPlay
The Audio play event structure.
gBool gaudioRecordInit(gU16 channel, gU32 frequency, ArrayDataFormat format)
Initialise (but not start) the Audio Recording sub-system.
void gaudioPlayPause(void)
Pause any currently playing sounds.
void gaudioPlay(GDataBuffer *paud)
Play the specified sample data.
GSourceHandle gaudioRecordGetSource(void)
Turn on sending results to the GEVENT sub-system.
gBool gaudioPlayWait(gDelay ms)
Wait for any currently playing sounds to complete.
enum ArrayDataFormat_e ArrayDataFormat
Sample data formats.
A Data Buffer Queue.
Definition: gqueue.h:78
The Audio play event structure.
Definition: gaudio.h:46
gU16 flags
The event flags.
Definition: gaudio.h:54
GEventType type
The type of this event (GEVENT_AUDIO_PLAY)
Definition: gaudio.h:50
The Audio record event structure.
Definition: gaudio.h:70
gU16 flags
The event flags.
Definition: gaudio.h:78
GEventType type
The type of this event (GEVENT_AUDIO_RECORD)
Definition: gaudio.h:74