comparison src/audio/mint/SDL_mintaudio.c @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents 14717b52abc0
children 4da1ee79c9af
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
38 #include "SDL_mintaudio_stfa.h" 38 #include "SDL_mintaudio_stfa.h"
39 39
40 /* The audio device */ 40 /* The audio device */
41 41
42 SDL_AudioDevice *SDL_MintAudio_device; 42 SDL_AudioDevice *SDL_MintAudio_device;
43 Uint8 *SDL_MintAudio_audiobuf[2]; /* Pointers to buffers */ 43 Uint8 *SDL_MintAudio_audiobuf[2]; /* Pointers to buffers */
44 unsigned long SDL_MintAudio_audiosize; /* Length of audio buffer=spec->size */ 44 unsigned long SDL_MintAudio_audiosize; /* Length of audio buffer=spec->size */
45 volatile unsigned short SDL_MintAudio_numbuf; /* Buffer to play */ 45 volatile unsigned short SDL_MintAudio_numbuf; /* Buffer to play */
46 volatile unsigned short SDL_MintAudio_mutex; 46 volatile unsigned short SDL_MintAudio_mutex;
47 volatile unsigned long SDL_MintAudio_clocktics; 47 volatile unsigned long SDL_MintAudio_clocktics;
48 cookie_stfa_t *SDL_MintAudio_stfa; 48 cookie_stfa_t *SDL_MintAudio_stfa;
49 49
50 /* MiNT thread variables */ 50 /* MiNT thread variables */
51 SDL_bool SDL_MintAudio_mint_present; 51 SDL_bool SDL_MintAudio_mint_present;
52 SDL_bool SDL_MintAudio_quit_thread; 52 SDL_bool SDL_MintAudio_quit_thread;
53 SDL_bool SDL_MintAudio_thread_finished; 53 SDL_bool SDL_MintAudio_thread_finished;
54 long SDL_MintAudio_thread_pid; 54 long SDL_MintAudio_thread_pid;
55 55
56 /* The callback function, called by each driver whenever needed */ 56 /* The callback function, called by each driver whenever needed */
57 57
58 void SDL_MintAudio_Callback(void) 58 void
59 { 59 SDL_MintAudio_Callback (void)
60 Uint8 *buffer; 60 {
61 SDL_AudioDevice *audio = SDL_MintAudio_device; 61 Uint8 *buffer;
62 62 SDL_AudioDevice *audio = SDL_MintAudio_device;
63 buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf]; 63
64 SDL_memset(buffer, audio->spec.silence, audio->spec.size); 64 buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
65 65 SDL_memset (buffer, audio->spec.silence, audio->spec.size);
66 if (audio->paused) 66
67 return; 67 if (audio->paused)
68 68 return;
69 if (audio->convert.needed) { 69
70 int silence; 70 if (audio->convert.needed) {
71 71 int silence;
72 if ( audio->convert.src_format == AUDIO_U8 ) { 72
73 silence = 0x80; 73 if (audio->convert.src_format == AUDIO_U8) {
74 } else { 74 silence = 0x80;
75 silence = 0; 75 } else {
76 } 76 silence = 0;
77 SDL_memset(audio->convert.buf, silence, audio->convert.len); 77 }
78 audio->spec.callback(audio->spec.userdata, 78 SDL_memset (audio->convert.buf, silence, audio->convert.len);
79 (Uint8 *)audio->convert.buf,audio->convert.len); 79 audio->spec.callback (audio->spec.userdata,
80 SDL_ConvertAudio(&audio->convert); 80 (Uint8 *) audio->convert.buf,
81 SDL_memcpy(buffer, audio->convert.buf, audio->convert.len_cvt); 81 audio->convert.len);
82 } else { 82 SDL_ConvertAudio (&audio->convert);
83 audio->spec.callback(audio->spec.userdata, buffer, audio->spec.size); 83 SDL_memcpy (buffer, audio->convert.buf, audio->convert.len_cvt);
84 } 84 } else {
85 audio->spec.callback (audio->spec.userdata, buffer, audio->spec.size);
86 }
85 } 87 }
86 88
87 /* Add a new frequency/clock/predivisor to the current list */ 89 /* Add a new frequency/clock/predivisor to the current list */
88 void SDL_MintAudio_AddFrequency(_THIS, Uint32 frequency, Uint32 clock, 90 void
89 Uint32 prediv, int gpio_bits) 91 SDL_MintAudio_AddFrequency (_THIS, Uint32 frequency, Uint32 clock,
90 { 92 Uint32 prediv, int gpio_bits)
91 int i, p; 93 {
92 94 int i, p;
93 if (MINTAUDIO_freqcount==MINTAUDIO_maxfreqs) { 95
94 return; 96 if (MINTAUDIO_freqcount == MINTAUDIO_maxfreqs) {
95 } 97 return;
96 98 }
97 /* Search where to insert the frequency (highest first) */ 99
98 for (p=0; p<MINTAUDIO_freqcount; p++) { 100 /* Search where to insert the frequency (highest first) */
99 if (frequency > MINTAUDIO_frequencies[p].frequency) { 101 for (p = 0; p < MINTAUDIO_freqcount; p++) {
100 break; 102 if (frequency > MINTAUDIO_frequencies[p].frequency) {
101 } 103 break;
102 } 104 }
103 105 }
104 /* Put all following ones farer */ 106
105 if (MINTAUDIO_freqcount>0) { 107 /* Put all following ones farer */
106 for (i=MINTAUDIO_freqcount; i>p; i--) { 108 if (MINTAUDIO_freqcount > 0) {
107 SDL_memcpy(&MINTAUDIO_frequencies[i], &MINTAUDIO_frequencies[i-1], sizeof(mint_frequency_t)); 109 for (i = MINTAUDIO_freqcount; i > p; i--) {
108 } 110 SDL_memcpy (&MINTAUDIO_frequencies[i],
109 } 111 &MINTAUDIO_frequencies[i - 1],
110 112 sizeof (mint_frequency_t));
111 /* And insert new one */ 113 }
112 MINTAUDIO_frequencies[p].frequency = frequency; 114 }
113 MINTAUDIO_frequencies[p].masterclock = clock; 115
114 MINTAUDIO_frequencies[p].predivisor = prediv; 116 /* And insert new one */
115 MINTAUDIO_frequencies[p].gpio_bits = gpio_bits; 117 MINTAUDIO_frequencies[p].frequency = frequency;
116 118 MINTAUDIO_frequencies[p].masterclock = clock;
117 MINTAUDIO_freqcount++; 119 MINTAUDIO_frequencies[p].predivisor = prediv;
120 MINTAUDIO_frequencies[p].gpio_bits = gpio_bits;
121
122 MINTAUDIO_freqcount++;
118 } 123 }
119 124
120 /* Search for the nearest frequency */ 125 /* Search for the nearest frequency */
121 int SDL_MintAudio_SearchFrequency(_THIS, int desired_freq) 126 int
122 { 127 SDL_MintAudio_SearchFrequency (_THIS, int desired_freq)
123 int i; 128 {
124 129 int i;
125 /* Only 1 freq ? */ 130
126 if (MINTAUDIO_freqcount==1) { 131 /* Only 1 freq ? */
127 return 0; 132 if (MINTAUDIO_freqcount == 1) {
128 } 133 return 0;
129 134 }
130 /* Check the array */ 135
131 for (i=0; i<MINTAUDIO_freqcount; i++) { 136 /* Check the array */
132 if (desired_freq >= ((MINTAUDIO_frequencies[i].frequency+ 137 for (i = 0; i < MINTAUDIO_freqcount; i++) {
133 MINTAUDIO_frequencies[i+1].frequency)>>1)) { 138 if (desired_freq >= ((MINTAUDIO_frequencies[i].frequency +
134 return i; 139 MINTAUDIO_frequencies[i + 1].frequency) >> 1)) {
135 } 140 return i;
136 } 141 }
137 142 }
138 /* Not in the array, give the latest */ 143
139 return MINTAUDIO_freqcount-1; 144 /* Not in the array, give the latest */
145 return MINTAUDIO_freqcount - 1;
140 } 146 }
141 147
142 /* The thread function, used under MiNT with xbios */ 148 /* The thread function, used under MiNT with xbios */
143 int SDL_MintAudio_Thread(long param) 149 int
144 { 150 SDL_MintAudio_Thread (long param)
145 SndBufPtr pointers; 151 {
146 SDL_bool buffers_filled[2] = {SDL_FALSE, SDL_FALSE}; 152 SndBufPtr pointers;
147 153 SDL_bool buffers_filled[2] = { SDL_FALSE, SDL_FALSE };
148 SDL_MintAudio_thread_finished = SDL_FALSE; 154
149 while (!SDL_MintAudio_quit_thread) { 155 SDL_MintAudio_thread_finished = SDL_FALSE;
150 if (Buffptr(&pointers)!=0) 156 while (!SDL_MintAudio_quit_thread) {
151 continue; 157 if (Buffptr (&pointers) != 0)
152 158 continue;
153 if (( (unsigned long)pointers.play>=(unsigned long)SDL_MintAudio_audiobuf[0]) 159
154 && ( (unsigned long)pointers.play<=(unsigned long)SDL_MintAudio_audiobuf[1])) 160 if (((unsigned long) pointers.play >=
155 { 161 (unsigned long) SDL_MintAudio_audiobuf[0])
156 /* DMA is reading buffer #0, setup buffer #1 if not already done */ 162 && ((unsigned long) pointers.play <=
157 if (!buffers_filled[1]) { 163 (unsigned long) SDL_MintAudio_audiobuf[1])) {
158 SDL_MintAudio_numbuf = 1; 164 /* DMA is reading buffer #0, setup buffer #1 if not already done */
159 SDL_MintAudio_Callback(); 165 if (!buffers_filled[1]) {
160 Setbuffer(0, SDL_MintAudio_audiobuf[1], SDL_MintAudio_audiobuf[1] + SDL_MintAudio_audiosize); 166 SDL_MintAudio_numbuf = 1;
161 buffers_filled[1]=SDL_TRUE; 167 SDL_MintAudio_Callback ();
162 buffers_filled[0]=SDL_FALSE; 168 Setbuffer (0, SDL_MintAudio_audiobuf[1],
163 } 169 SDL_MintAudio_audiobuf[1] +
164 } else { 170 SDL_MintAudio_audiosize);
165 /* DMA is reading buffer #1, setup buffer #0 if not already done */ 171 buffers_filled[1] = SDL_TRUE;
166 if (!buffers_filled[0]) { 172 buffers_filled[0] = SDL_FALSE;
167 SDL_MintAudio_numbuf = 0; 173 }
168 SDL_MintAudio_Callback(); 174 } else {
169 Setbuffer(0, SDL_MintAudio_audiobuf[0], SDL_MintAudio_audiobuf[0] + SDL_MintAudio_audiosize); 175 /* DMA is reading buffer #1, setup buffer #0 if not already done */
170 buffers_filled[0]=SDL_TRUE; 176 if (!buffers_filled[0]) {
171 buffers_filled[1]=SDL_FALSE; 177 SDL_MintAudio_numbuf = 0;
172 } 178 SDL_MintAudio_Callback ();
173 } 179 Setbuffer (0, SDL_MintAudio_audiobuf[0],
174 180 SDL_MintAudio_audiobuf[0] +
175 usleep(100); 181 SDL_MintAudio_audiosize);
176 } 182 buffers_filled[0] = SDL_TRUE;
177 SDL_MintAudio_thread_finished = SDL_TRUE; 183 buffers_filled[1] = SDL_FALSE;
178 return 0; 184 }
179 } 185 }
180 186
181 void SDL_MintAudio_WaitThread(void) 187 usleep (100);
182 { 188 }
183 if (!SDL_MintAudio_mint_present) 189 SDL_MintAudio_thread_finished = SDL_TRUE;
184 return; 190 return 0;
185 191 }
186 if (SDL_MintAudio_thread_finished) 192
187 return; 193 void
188 194 SDL_MintAudio_WaitThread (void)
189 SDL_MintAudio_quit_thread = SDL_TRUE; 195 {
190 while (!SDL_MintAudio_thread_finished) { 196 if (!SDL_MintAudio_mint_present)
191 Syield(); 197 return;
192 } 198
193 } 199 if (SDL_MintAudio_thread_finished)
200 return;
201
202 SDL_MintAudio_quit_thread = SDL_TRUE;
203 while (!SDL_MintAudio_thread_finished) {
204 Syield ();
205 }
206 }
207
208 /* vi: set ts=4 sw=4 expandtab: */