aboutsummaryrefslogtreecommitdiffstats
path: root/src/gaudio/gaudio.c
blob: c08f1d85d04b372b84b76929e955d793b84f5cbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://ugfx.org/license.html
 */

#include "../../gfx.h"

#if GFX_USE_GAUDIO

#if GAUDIO_NEED_PLAY
	#include "gaudio_driver_play.h"

	static gfxQueueASync	playList;
	static gfxSem			playComplete;
	static uint16_t			playFlags;
		#define PLAYFLG_USEEVENTS	0x0001
		#define PLAYFLG_PLAYING		0x0002
		#define PLAYFLG_ISINIT		0x0004
	#if GFX_USE_GEVENT
		static GTimer playTimer;
		static void PlayTimerCallback(void *param);
	#endif
#endif

#if GAUDIO_NEED_RECORD
	#include "gaudio_driver_record.h"

	static gfxQueueGSync	recordList;
	static uint16_t			recordFlags;
		#define RECORDFLG_USEEVENTS		0x0001
		#define RECORDFLG_RECORDING		0x0002
		#define RECORDFLG_STALLED		0x0004
		#define RECORDFLG_ISINIT		0x0008
	#if GFX_USE_GEVENT
		static GTimer recordTimer;
		static void RecordTimerCallback(void *param);
	#endif
#endif


void _gaudioInit(void)
{
	#if GAUDIO_NEED_PLAY
		gfxQueueASyncInit(&playList);
		#if GFX_USE_GEVENT
			gtimerInit(&playTimer);
		#endif
		gfxSemInit(&playComplete, 0, 0);
	#endif
	#if GAUDIO_NEED_RECORD
		gfxQueueGSyncInit(&recordList);
		#if GFX_USE_GEVENT
			gtimerInit(&recordTimer);
		#endif
	#endif
}

void _gaudioDeinit(void)
{
	#if GAUDIO_NEED_PLAY
		gfxQueueASyncDeinit(&playList);
		#if GFX_USE_GEVENT
			gtimerDeinit(&playTimer);
		#endif
		gfxSemDestroy(&playComplete);
	#endif
	#if GAUDIO_NEED_RECORD
		gfxQueueGSyncDeinit(&recordList);
		#if GFX_USE_GEVENT
			gtimerDeinit(&recordTimer);
		#endif
	#endif
}

#if GAUDIO_NEED_PLAY

	gBool gaudioPlayInit(uint16_t channel, uint32_t frequency, ArrayDataFormat format) {
		gaudioPlayStop();
		playFlags &= ~PLAYFLG_ISINIT;
		if (!gaudio_play_lld_init(channel, frequency, format))
			return gFalse;
		playFlags |= PLAYFLG_ISINIT;
		return gTrue;
	}

	void gaudioPlay(GDataBuffer *pd) {
		if (!(playFlags & PLAYFLG_ISINIT)) {
			// Oops - init failed - return it directly to the free-list
			if (pd) {
				gfxBufferRelease(pd);
				gfxYield();				// Make sure we get no endless cpu hogging loops
			}
			return;
		}

		if (pd)
			gfxQueueASyncPut(&playList, (gfxQueueASyncItem *)pd);
		playFlags |= PLAYFLG_PLAYING;
		gaudio_play_lld_start();
	}

	void gaudioPlayPause(void) {
		if ((playFlags & (PLAYFLG_ISINIT|PLAYFLG_PLAYING)) == (PLAYFLG_ISINIT|PLAYFLG_PLAYING))
			gaudio_play_lld_stop();
	}

	void gaudioPlayStop(void) {
		GDataBuffer	*pd;

		if (playFlags & PLAYFLG_PLAYING)
			gaudio_play_lld_stop();
		while((pd = (GDataBuffer *)gfxQueueASyncGet(&playList)))
			gfxBufferRelease(pd);
	}

	gBool gaudioPlaySetVolume(uint8_t vol) {
		return gaudio_play_lld_set_volume(vol);
	}

	gBool gaudioPlayWait(gDelay ms) {
		if (!(playFlags & PLAYFLG_PLAYING))
			return gTrue;
		return gfxSemWait(&playComplete, ms);
	}

	#if GFX_USE_GEVENT
		static void PlayTimerCallback(void *param) {
			(void) param;
			GSourceListener	*psl;
			GEventAudioPlay	*pe;

			psl = 0;
			while ((psl = geventGetSourceListener((GSourceHandle)&playTimer, psl))) {
				if (!(pe = (GEventAudioPlay *)geventGetEventBuffer(psl))) {
					// This listener is missing - save this.
					psl->srcflags |= GAUDIO_PLAY_LOSTEVENT;
					continue;
				}

				pe->type = GEVENT_AUDIO_PLAY;
				pe->flags = psl->srcflags;
				psl->srcflags = 0;
				if ((playFlags & PLAYFLG_PLAYING))
					pe->flags |= GAUDIO_PLAY_PLAYING;
				if (gfxBufferIsAvailable())
					pe->flags |= GAUDIO_PLAY_FREEBLOCK;
				geventSendEvent(psl);
			}
		}

		GSourceHandle gaudioPlayGetSource(void) {
			if (!gtimerIsActive(&playTimer))
				gtimerStart(&playTimer, PlayTimerCallback, 0, gTrue, gDelayForever);
			playFlags |= PLAYFLG_USEEVENTS;
			return (GSourceHandle)&playTimer;
		}
	#endif

	/**
	 * Routines provided for use by drivers.
	 */

	GDataBuffer *gaudioPlayGetDataBlockI(void) {
		return (GDataBuffer *)gfxQueueASyncGetI(&playList);
	}

	void gaudioPlayReleaseDataBlockI(GDataBuffer *pd) {
		gfxBufferReleaseI(pd);
		#if GFX_USE_GEVENT
			if (playFlags & PLAYFLG_USEEVENTS)
				gtimerJabI(&playTimer);
		#endif
	}

	void gaudioPlayDoneI(void) {
		playFlags &= ~PLAYFLG_PLAYING;
		#if GFX_USE_GEVENT
			if (playFlags & PLAYFLG_USEEVENTS)
				gtimerJabI(&playTimer);
		#endif
		gfxSemSignalI(&playComplete);			// This should really be gfxSemSignalAllI(&playComplete);
	}
#endif

#if GAUDIO_NEED_RECORD
	gBool gaudioRecordInit(uint16_t channel, uint32_t frequency, ArrayDataFormat format) {
		gaudioRecordStop();
		recordFlags &= ~RECORDFLG_ISINIT;
		if (!gaudio_record_lld_init(channel, frequency, format))
			return gFalse;
		recordFlags |= RECORDFLG_ISINIT;
		return gTrue;
	}

	void gaudioRecordStart(void) {
		if (!(recordFlags & RECORDFLG_ISINIT))
			return;							// Oops - init failed

		recordFlags |= RECORDFLG_RECORDING;
		recordFlags &= ~RECORDFLG_STALLED;
		gaudio_record_lld_start();
	}

	void gaudioRecordStop(void) {
		GDataBuffer	*pd;

		if ((recordFlags & (RECORDFLG_RECORDING|RECORDFLG_STALLED)) == RECORDFLG_RECORDING)
			gaudio_record_lld_stop();
		recordFlags &= ~(RECORDFLG_RECORDING|RECORDFLG_STALLED);
		while((pd = (GDataBuffer *)gfxQueueGSyncGet(&recordList, gDelayNone)))
			gfxBufferRelease(pd);
	}

	GDataBuffer *gaudioRecordGetData(gDelay ms) {
		return (GDataBuffer *)gfxQueueGSyncGet(&recordList, ms);
	}

	#if GFX_USE_GEVENT
		static void RecordTimerCallback(void *param) {
			(void) param;
			GSourceListener		*psl;
			GEventAudioRecord	*pe;

			psl = 0;
			while ((psl = geventGetSourceListener((GSourceHandle)&recordTimer, psl))) {
				if (!(pe = (GEventAudioRecord *)geventGetEventBuffer(psl))) {
					// This listener is missing - save this.
					psl->srcflags |= GAUDIO_RECORD_LOSTEVENT;
					continue;
				}
				pe->type = GEVENT_AUDIO_RECORD;
				pe->flags = psl->srcflags;
				psl->srcflags = 0;
				if ((recordFlags & RECORDFLG_RECORDING))
					pe->flags |= GAUDIO_RECORD_RECORDING;
				if ((recordFlags & RECORDFLG_STALLED))
					pe->flags |= GAUDIO_RECORD_STALL;
				if (!gfxQueueGSyncIsEmpty(&recordList))
					pe->flags |= GAUDIO_RECORD_GOTBUFFER;
				geventSendEvent(psl);
			}
		}

		GSourceHandle gaudioRecordGetSource(void) {
			if (!gtimerIsActive(&recordTimer))
				gtimerStart(&recordTimer, RecordTimerCallback, 0, gTrue, gDelayForever);
			recordFlags |= RECORDFLG_USEEVENTS;
			return (GSourceHandle)&recordTimer;
		}
	#endif

	/**
	 * Routines provided for use by drivers.
	 */

	void gaudioRecordSaveDataBlockI(GDataBuffer *paud) {
		gfxQueueGSyncPutI(&recordList, (gfxQueueGSyncItem *)paud);
		#if GFX_USE_GEVENT
			if (recordFlags & RECORDFLG_USEEVENTS)
				gtimerJabI(&recordTimer);
		#endif
	}

	void gaudioRecordDoneI(void) {
		recordFlags |= RECORDFLG_STALLED;
		#if GFX_USE_GEVENT
			if (recordFlags & RECORDFLG_USEEVENTS)
				gtimerJabI(&recordTimer);
		#endif
	}
#endif

#endif /* GFX_USE_GAUDIO */