2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "SDL_config.h"
25 Audio interrupt variables and callback function
32 #include <mint/osbind.h>
33 #include <mint/falcon.h>
34 #include <mint/mintbind.h>
35 #include <mint/cookie.h>
37 #include "SDL_audio.h"
38 #include "SDL_mintaudio.h"
39 #include "SDL_mintaudio_stfa.h"
41 /* The audio device */
43 SDL_AudioDevice *SDL_MintAudio_device;
44 Uint8 *SDL_MintAudio_audiobuf[2]; /* Pointers to buffers */
45 unsigned long SDL_MintAudio_audiosize; /* Length of audio buffer=spec->size */
46 volatile unsigned short SDL_MintAudio_numbuf; /* Buffer to play */
47 volatile unsigned short SDL_MintAudio_mutex;
48 volatile unsigned long SDL_MintAudio_clocktics;
49 cookie_stfa_t *SDL_MintAudio_stfa;
50 unsigned short SDL_MintAudio_hasfpu;
52 /* MiNT thread variables */
53 SDL_bool SDL_MintAudio_mint_present;
54 SDL_bool SDL_MintAudio_quit_thread;
55 SDL_bool SDL_MintAudio_thread_finished;
56 long SDL_MintAudio_thread_pid;
58 /* The callback function, called by each driver whenever needed */
60 void SDL_MintAudio_Callback(void)
63 SDL_AudioDevice *audio = SDL_MintAudio_device;
65 buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
66 SDL_memset(buffer, audio->spec.silence, audio->spec.size);
71 if (audio->convert.needed) {
74 if ( audio->convert.src_format == AUDIO_U8 ) {
79 SDL_memset(audio->convert.buf, silence, audio->convert.len);
80 audio->spec.callback(audio->spec.userdata,
81 (Uint8 *)audio->convert.buf,audio->convert.len);
82 SDL_ConvertAudio(&audio->convert);
83 SDL_memcpy(buffer, audio->convert.buf, audio->convert.len_cvt);
85 audio->spec.callback(audio->spec.userdata, buffer, audio->spec.size);
89 /* Add a new frequency/clock/predivisor to the current list */
90 void SDL_MintAudio_AddFrequency(_THIS, Uint32 frequency, Uint32 clock,
91 Uint32 prediv, int gpio_bits)
95 if (MINTAUDIO_freqcount==MINTAUDIO_maxfreqs) {
99 /* Search where to insert the frequency (highest first) */
100 for (p=0; p<MINTAUDIO_freqcount; p++) {
101 if (frequency > MINTAUDIO_frequencies[p].frequency) {
106 /* Put all following ones farer */
107 if (MINTAUDIO_freqcount>0) {
108 for (i=MINTAUDIO_freqcount; i>p; i--) {
109 SDL_memcpy(&MINTAUDIO_frequencies[i], &MINTAUDIO_frequencies[i-1], sizeof(mint_frequency_t));
113 /* And insert new one */
114 MINTAUDIO_frequencies[p].frequency = frequency;
115 MINTAUDIO_frequencies[p].masterclock = clock;
116 MINTAUDIO_frequencies[p].predivisor = prediv;
117 MINTAUDIO_frequencies[p].gpio_bits = gpio_bits;
119 MINTAUDIO_freqcount++;
122 /* Search for the nearest frequency */
123 int SDL_MintAudio_SearchFrequency(_THIS, int desired_freq)
128 if (MINTAUDIO_freqcount==1) {
132 /* Check the array */
133 for (i=0; i<MINTAUDIO_freqcount; i++) {
134 if (desired_freq >= ((MINTAUDIO_frequencies[i].frequency+
135 MINTAUDIO_frequencies[i+1].frequency)>>1)) {
140 /* Not in the array, give the latest */
141 return MINTAUDIO_freqcount-1;
144 /* Check if FPU is present */
145 void SDL_MintAudio_CheckFpu(void)
149 SDL_MintAudio_hasfpu = 0;
150 if (Getcookie(C__FPU, &cookie_fpu) != C_FOUND) {
153 switch ((cookie_fpu>>16)&0xfffe) {
159 SDL_MintAudio_hasfpu = 1;
164 /* The thread function, used under MiNT with xbios */
165 int SDL_MintAudio_Thread(long param)
168 SDL_bool buffers_filled[2] = {SDL_FALSE, SDL_FALSE};
170 SDL_MintAudio_thread_finished = SDL_FALSE;
171 while (!SDL_MintAudio_quit_thread) {
172 if (Buffptr(&pointers)!=0)
175 if (( (unsigned long)pointers.play>=(unsigned long)SDL_MintAudio_audiobuf[0])
176 && ( (unsigned long)pointers.play<=(unsigned long)SDL_MintAudio_audiobuf[1]))
178 /* DMA is reading buffer #0, setup buffer #1 if not already done */
179 if (!buffers_filled[1]) {
180 SDL_MintAudio_numbuf = 1;
181 SDL_MintAudio_Callback();
182 Setbuffer(0, SDL_MintAudio_audiobuf[1], SDL_MintAudio_audiobuf[1] + SDL_MintAudio_audiosize);
183 buffers_filled[1]=SDL_TRUE;
184 buffers_filled[0]=SDL_FALSE;
187 /* DMA is reading buffer #1, setup buffer #0 if not already done */
188 if (!buffers_filled[0]) {
189 SDL_MintAudio_numbuf = 0;
190 SDL_MintAudio_Callback();
191 Setbuffer(0, SDL_MintAudio_audiobuf[0], SDL_MintAudio_audiobuf[0] + SDL_MintAudio_audiosize);
192 buffers_filled[0]=SDL_TRUE;
193 buffers_filled[1]=SDL_FALSE;
199 SDL_MintAudio_thread_finished = SDL_TRUE;
203 void SDL_MintAudio_WaitThread(void)
205 if (!SDL_MintAudio_mint_present)
208 if (SDL_MintAudio_thread_finished)
211 SDL_MintAudio_quit_thread = SDL_TRUE;
212 while (!SDL_MintAudio_thread_finished) {