19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL - Simple DirectMedia Layer 39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Copyright (C) 1997-2012 Sam Lantinga 49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is free software; you can redistribute it and/or 69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall modify it under the terms of the GNU Lesser General Public 79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License as published by the Free Software Foundation; either 89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall version 2.1 of the License, or (at your option) any later version. 99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is distributed in the hope that it will be useful, 119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall but WITHOUT ANY WARRANTY; without even the implied warranty of 129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Lesser General Public License for more details. 149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall You should have received a copy of the GNU Lesser General Public 169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License along with this library; if not, write to the Free Software 179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sam Lantinga 209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall slouken@libsdl.org 219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h" 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Allow access to a raw mixing buffer */ 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <nds.h> 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL.h" 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_endian.h" 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h" 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h" 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiomem.h" 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h" 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_ndsaudio.h" 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "soundcommon.h" 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */ 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NDS_OpenAudio(_THIS, SDL_AudioSpec *spec); 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NDS_WaitAudio(_THIS); 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NDS_PlayAudio(_THIS); 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *NDS_GetAudioBuf(_THIS); 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NDS_CloseAudio(_THIS); 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */ 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallu32 framecounter = 0,soundoffset = 0; 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *sdl_nds_audiodevice; 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//void SoundMixCallback(void *stream,u32 size) 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//{ 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// //printf("SoundMixCallback\n"); 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Uint8 *buffer; 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// buffer = sdl_nds_audiodevice->hidden->mixbuf; 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// memset(buffer, sdl_nds_audiodevice->spec.silence, size); 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// if (!sdl_nds_audiodevice->paused){ 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// //if (sdl_nds_audiodevice->convert.needed) { 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // int silence; 629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// 639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // if (sdl_nds_audiodevice->convert.src_format == AUDIO_U8 ) { 649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // silence = 0x80; 659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // } else { 669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // silence = 0; 679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // } 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // memset(sdl_nds_audiodevice->convert.buf, silence, sdl_nds_audiodevice->convert.len); 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // sdl_nds_audiodevice->spec.callback(sdl_nds_audiodevice->spec.userdata, 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // (Uint8 *)sdl_nds_audiodevice->convert.buf,sdl_nds_audiodevice->convert.len); 719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // SDL_ConvertAudio(&sdl_nds_audiodevice->convert); 729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // memcpy(buffer, sdl_nds_audiodevice->convert.buf, sdl_nds_audiodevice->convert.len_cvt); 739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// //} else 749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// { 759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// sdl_nds_audiodevice->spec.callback(sdl_nds_audiodevice->spec.userdata, buffer, size); 769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// //memcpy((Sint16 *)stream,buffer, size); 779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// } 789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// 799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// } 809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// 819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// if(soundsystem->format == 8) 829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// { 839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// int i; 849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// s32 *buffer32 = (s32 *)buffer; 859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// s32 *stream32 = (s32 *)stream; 869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// for(i=0;i<size/4;i++){ *stream32++ = buffer32[i] ^ 0x80808080;} 879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// //for(i = 0; i < size; i++) 889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // ((s8*)stream)[i]=(buffer[i]^0x80); 899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// } 909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// else 919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// { 929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// int i; 939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// for(i = 0; i < size; i++){ 949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// //((short*)stream)[i] =(short)buffer[i] << 8; // sound 8bit ---> buffer 16bit 959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// //if (buffer[i] &0x80) 969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// //((Sint16*)stream)[i] = 0xff00 | buffer[i]; 979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// ((Sint16*)stream)[i] = (buffer[i] - 128) << 8; 989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// 999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// //else 1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // ((Sint16*)stream)[i] = buffer[i]; 1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// } 1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// //register signed char *pSrc =buffer; 1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// //register short *pDest =stream; 1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// //int x; 1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // for (x=size; x>0; x--) 1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // { 1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // register short temp = (((short)*pSrc)-128)<<8; 1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // pSrc++; 1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // *pDest++ = temp; 1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// // } 1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// 1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// //memcpy((Sint16 *)stream,buffer, size); 1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// } 1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//} 1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SoundMixCallback(void *stream,u32 len) 1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_AudioDevice *audio = (SDL_AudioDevice *)sdl_nds_audiodevice; 1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Silence the buffer, since it's ours */ 1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(stream, audio->spec.silence, len); 1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Only do soemthing if audio is enabled */ 1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ! audio->enabled ) 1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ! audio->paused ) { 1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( audio->convert.needed ) { 1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall //fprintf(stderr,"converting audio\n"); 1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_mutexP(audio->mixer_lock); 1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (*audio->spec.callback)(audio->spec.userdata, 1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (Uint8 *)audio->convert.buf,audio->convert.len); 1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_mutexV(audio->mixer_lock); 1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_ConvertAudio(&audio->convert); 1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memcpy(stream,audio->convert.buf,audio->convert.len_cvt); 1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_mutexP(audio->mixer_lock); 1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (*audio->spec.callback)(audio->spec.userdata, 1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (Uint8 *)stream, len); 1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_mutexV(audio->mixer_lock); 1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid MixSound(void) 1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int remain; 1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(soundsystem->format == 8) 1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if((soundsystem->soundcursor + soundsystem->numsamples) > soundsystem->buffersize) 1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SoundMixCallback(&soundsystem->mixbuffer[soundsystem->soundcursor],soundsystem->buffersize - soundsystem->soundcursor); 1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall remain = soundsystem->numsamples - (soundsystem->buffersize - soundsystem->soundcursor); 1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SoundMixCallback(soundsystem->mixbuffer,remain); 1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else 1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SoundMixCallback(&soundsystem->mixbuffer[soundsystem->soundcursor],soundsystem->numsamples); 1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else 1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if((soundsystem->soundcursor + soundsystem->numsamples) > (soundsystem->buffersize >> 1)) 1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SoundMixCallback(&soundsystem->mixbuffer[soundsystem->soundcursor << 1],(soundsystem->buffersize >> 1) - soundsystem->soundcursor); 1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall remain = soundsystem->numsamples - ((soundsystem->buffersize >> 1) - soundsystem->soundcursor); 1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SoundMixCallback(soundsystem->mixbuffer,remain); 1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else 1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SoundMixCallback(&soundsystem->mixbuffer[soundsystem->soundcursor << 1],soundsystem->numsamples); 1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid InterruptHandler(void) 1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall framecounter++; 1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid FiFoHandler(void) 1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall u32 command; 1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while ( !(REG_IPC_FIFO_CR & (IPC_FIFO_RECV_EMPTY)) ) 1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall command = REG_IPC_FIFO_RX; 1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch(command) 1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case FIFO_NONE: 1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case UPDATEON_ARM9: 1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall REG_IME = 0; 1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MixSound(); 1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall REG_IME = 1; 1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SendCommandToArm7(MIXCOMPLETE_ONARM9); 1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void) 2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(1); 2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device) 2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex) 2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_AudioDevice *this; 2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Initialize all variables that we clean on shutdown */ 2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice)); 2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( this ) { 2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(this, 0, (sizeof *this)); 2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->hidden = (struct SDL_PrivateAudioData *) 2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_malloc((sizeof *this->hidden)); 2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (this == NULL) || (this->hidden == NULL) ) { 2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_OutOfMemory(); 2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( this ) { 2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(this); 2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(this->hidden, 0, (sizeof *this->hidden)); 2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the function pointers */ 2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->OpenAudio = NDS_OpenAudio; 2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->WaitAudio = NDS_WaitAudio; 2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->PlayAudio = NDS_PlayAudio; 2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->GetAudioBuf = NDS_GetAudioBuf; 2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->CloseAudio = NDS_CloseAudio; 2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->free = Audio_DeleteDevice; 2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//fprintf(stderr,"Audio_CreateDevice\n"); 2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return this; 2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap NDSAUD_bootstrap = { 2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall "nds", "NDS audio", 2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Audio_Available, Audio_CreateDevice 2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; 2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid static NDS_WaitAudio(_THIS) 2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall //printf("NDS_WaitAudio\n"); 2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NDS_PlayAudio(_THIS) 2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall //printf("playing audio\n"); 2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (this->paused) 2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *NDS_GetAudioBuf(_THIS) 2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return NULL;//(this->hidden->mixbuf); 2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NDS_CloseAudio(_THIS) 2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* if ( this->hidden->mixbuf != NULL ) { 2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_FreeAudioMem(this->hidden->mixbuf); 2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->hidden->mixbuf = NULL; 2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall }*/ 2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NDS_OpenAudio(_THIS, SDL_AudioSpec *spec) 2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall //printf("NDS_OpenAudio\n"); 2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int format = 0; 2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall //switch(spec->format&0xff) { 2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall //case 8: spec->format = AUDIO_S8;format=8; break; 2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall //case 16: spec->format = AUDIO_S16LSB;format=16; break; 2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall //default: 2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // SDL_SetError("Unsupported audio format"); 2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // return(-1); 2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall //} 2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch (spec->format&~0x1000) { 2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_S8: 2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Signed 8-bit audio supported */ 2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format=8; 2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_U8: 2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->format ^= 0x80;format=8; 2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_U16: 3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Unsigned 16-bit audio unsupported, convert to S16 */ 3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->format ^=0x8000;format=16; 3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case AUDIO_S16: 3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Signed 16-bit audio supported */ 3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format=16; 3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Update the fragment size as size in bytes */ 3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_CalculateAudioSpec(spec); 3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Allocate mixing buffer */ 3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall //this->hidden->mixlen = spec->size; 3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall //this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); 3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall //if ( this->hidden->mixbuf == NULL ) { 3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // SDL_SetError("Out of Memory"); 3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall // return(-1); 3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall //} 3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_NDSAudio_mutex = 0; 3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sdl_nds_audiodevice=this; 3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall irqInit(); 3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall irqSet(IRQ_VBLANK,&InterruptHandler); 3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall irqSet(IRQ_FIFO_NOT_EMPTY,&FiFoHandler); 3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall irqEnable(IRQ_FIFO_NOT_EMPTY); 3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall REG_IPC_FIFO_CR = IPC_FIFO_ENABLE | IPC_FIFO_SEND_CLEAR | IPC_FIFO_RECV_IRQ; 3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SoundSystemInit(spec->freq,spec->size,0,format); 3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SoundStartMixer(); 3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(1); 3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 336