aboutsummaryrefslogtreecommitdiffstats
path: root/src/gaudout/gaudout.c
blob: 275677c877e709784c0379abd3f7ad15b7c73d9b (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
/*
 * 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
 */

/**
 * @file    src/gaudout/gaudout.c
 * @brief   GAUDOUT sub-system code.
 *
 * @addtogroup GAUDOUT
 * @{
 */
#include "gfx.h"

#if GFX_USE_GAUDOUT || defined(__DOXYGEN__)

#include "src/gaudout/driver.h"

static gfxQueueASync	playlist;
static gfxQueueGSync	freelist;

static uint16_t			audFlags;
	#define AUDOUTFLG_RUNNING		0x0001
	#define AUDOUTFLG_USE_EVENTS	0x0002

#if GFX_USE_GEVENT
	static GTimer AudGTimer;

	static void AudGTimerCallback(void *param) {
		(void) param;
		GSourceListener	*psl;
		GEventADC		*pe;

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

			pe->type = GEVENT_AUDIO_IN;
			pe->channel = aud.channel;
			pe->count = lastcount;
			pe->buffer = lastbuffer;
			pe->flags = psl->srcflags;
			psl->srcflags = 0;
			geventSendEvent(psl);
		}
	}
#endif


void _gaudoutInit(void)
{
	gfxQueueASyncInit(&playlist);
	gfxQueueGSyncInit(&freelist);
}

void _gaudoutDeinit(void)
{
	/* ToDo */
}

bool_t gaudioAllocBuffers(unsigned num, size_t size) {
	GAudioData *paud;

	if (num < 1)
		return FALSE;

	// Round up to a multiple of 4 to prevent problems with structure alignment
	size = (size + 3) & ~0x03;

	// Allocate the memory
	if (!(paud = gfxAlloc((size+sizeof(GAudioData)) * num)))
		return FALSE;

	// Add each of them to our free list
	for(;num--; paud = (GAudioData *)((char *)(paud+1)+size)) {
		paud->size = size;
		gfxQueueGSyncPut(&freelist, (gfxQueueGSyncItem *)paud);
	}

	return TRUE;
}

void gaudioReleaseBuffer(GAudioData *paud) {
	gfxQueueGSyncPut(&freelist, (gfxQueueGSyncItem *)paud);
}

GAudioData *gaudioGetBuffer(delaytime_t ms) {
	return (GAudioData *)gfxQueueGSyncGet(&freelist, ms);
}

bool_t gaudioPlayInit(uint16_t channel, uint32_t frequency, ArrayDataFormat format) {
	gaudioPlayStop();
	gaudout_lld_deinit();
	return gaudout_lld_init(channel, frequency, format);
}

void gaudioPlay(GAudioData *paud) {
	if (paud)
		gfxQueueASyncPut(&playlist, (gfxQueueASyncItem *)paud);
	gaudout_lld_start();
}

void gaudioPlayPause(void) {
	gaudout_lld_stop();
}

void gaudioPlayStop(void) {
	GAudioData	*paud;

	gaudout_lld_stop();
	while((paud = (GAudioData *)gfxQueueASyncGet(&playlist)))
		gfxQueueGSyncPut(&freelist, (gfxQueueGSyncItem *)paud);
}

bool_t gaudioPlaySetVolume(uint8_t vol) {
	return gaudout_lld_set_volume(vol);
}

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

GAudioData *gaudoutGetDataBlockI(void) {
	return (GAudioData *)gfxQueueASyncGetI(&playlist);
}

void gaudoutReleaseDataBlockI(GAudioData *paud) {
	gfxQueueGSyncPutI(&freelist, (gfxQueueGSyncItem *)paud);
}


#endif /* GFX_USE_GAUDOUT */
/** @} */