| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program 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 |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <string.h> |
| 21 |
#include "../ta-log.h" |
| 22 |
#include "sdlAudio.h" |
| 23 |
|
| 24 |
void taSDLProcess(void* inst, unsigned char* buf, int nframes) { |
| 25 |
TAAudioSDL* in=(TAAudioSDL*)inst; |
| 26 |
in->onProcess(buf,nframes); |
| 27 |
} |
| 28 |
|
| 29 |
void TAAudioSDL::onProcess(unsigned char* buf, int nframes) { |
| 30 |
unsigned int unframes=nframes/(sizeof(float)*MAX(1,desc.outChans)); |
| 31 |
if (nframes<0) { |
| 32 |
logE("nframes is negative! (%d)",nframes); |
| 33 |
return; |
| 34 |
} |
| 35 |
if (desc.outChans<1) { |
| 36 |
logE("outChans is less than 1!"); |
| 37 |
return; |
| 38 |
} |
| 39 |
for (int i=0; i<desc.inChans; i++) { |
| 40 |
if (unframes>desc.bufsize) { |
| 41 |
delete[] inBufs[i]; |
| 42 |
inBufs[i]=new float[unframes]; |
| 43 |
} |
| 44 |
} |
| 45 |
for (int i=0; i<desc.outChans; i++) { |
| 46 |
if (unframes>desc.bufsize) { |
| 47 |
delete[] outBufs[i]; |
| 48 |
outBufs[i]=new float[unframes]; |
| 49 |
} |
| 50 |
} |
| 51 |
if (audioProcCallback!=NULL) { |
| 52 |
if (midiIn!=NULL) midiIn->gather(); |
| 53 |
audioProcCallback(audioProcCallbackUser,inBufs,outBufs,desc.inChans,desc.outChans,unframes); |
| 54 |
} |
| 55 |
float* fbuf=(float*)buf; |
| 56 |
for (size_t j=0; j<unframes; j++) { |
| 57 |
for (size_t i=0; i<desc.outChans; i++) { |
| 58 |
fbuf[j*desc.outChans+i]=outBufs[i][j]; |
| 59 |
} |
| 60 |
} |
| 61 |
if (unframes!=desc.bufsize) { |
| 62 |
desc.bufsize=unframes; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void* TAAudioSDL::getContext() { |
| 67 |
return (void*)∾ |
| 68 |
} |
| 69 |
|
| 70 |
bool TAAudioSDL::quit() { |
| 71 |
if (!initialized) return false; |
| 72 |
|
| 73 |
if (ai!=0) { |
| 74 |
SDL_CloseAudioDevice(ai); |
| 75 |
} |
| 76 |
|
| 77 |
if (running) { |
| 78 |
running=false; |
| 79 |
} |
| 80 |
|
| 81 |
for (int i=0; i<desc.outChans; i++) { |
| 82 |
delete[] outBufs[i]; |
| 83 |
} |
| 84 |
|
| 85 |
delete[] outBufs; |
| 86 |
|
| 87 |
initialized=false; |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
bool TAAudioSDL::setRun(bool run) { |
| 92 |
if (!initialized) return false; |
| 93 |
SDL_PauseAudioDevice(ai,!run); |
| 94 |
running=run; |
| 95 |
return running; |
| 96 |
} |
| 97 |
|
| 98 |
std::vector<String> TAAudioSDL::listAudioDevices() { |
| 99 |
std::vector<String> ret; |
| 100 |
if (!audioSysStarted) { |
| 101 |
if (SDL_Init(SDL_INIT_AUDIO)<0) { |
| 102 |
logE("could not initialize SDL to list audio devices"); |
| 103 |
} else { |
| 104 |
audioSysStarted=true; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
int count=SDL_GetNumAudioDevices(false); |
| 109 |
if (count<0) return ret; |
| 110 |
|
| 111 |
for (int i=0; i<count; i++) { |
| 112 |
const char* devName=SDL_GetAudioDeviceName(i,false); |
| 113 |
if (devName!=NULL) { |
| 114 |
ret.push_back(String(devName)); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return ret; |
| 119 |
} |
| 120 |
|
| 121 |
bool TAAudioSDL::init(TAAudioDesc& request, TAAudioDesc& response) { |
| 122 |
if (initialized) { |
| 123 |
logE("audio already initialized"); |
| 124 |
return false; |
| 125 |
} |
| 126 |
if (!audioSysStarted) { |
| 127 |
if (SDL_Init(SDL_INIT_AUDIO)<0) { |
| 128 |
logE("could not initialize SDL"); |
| 129 |
return false; |
| 130 |
} |
| 131 |
audioSysStarted=true; |
| 132 |
} |
| 133 |
|
| 134 |
const char* audioDriver=SDL_GetCurrentAudioDriver(); |
| 135 |
if (audioDriver==NULL) { |
| 136 |
logD("SDL audio driver: NULL!"); |
| 137 |
} else { |
| 138 |
logD("SDL audio driver: %s",audioDriver); |
| 139 |
} |
| 140 |
|
| 141 |
desc=request; |
| 142 |
desc.outFormat=TA_AUDIO_FORMAT_F32; |
| 143 |
|
| 144 |
ac.freq=desc.rate; |
| 145 |
#ifdef TA_BIG_ENDIAN |
| 146 |
ac.format=AUDIO_F32MSB; |
| 147 |
#else |
| 148 |
ac.format=AUDIO_F32; |
| 149 |
#endif |
| 150 |
ac.channels=desc.outChans; |
| 151 |
ac.samples=desc.bufsize; |
| 152 |
ac.callback=taSDLProcess; |
| 153 |
ac.userdata=this; |
| 154 |
|
| 155 |
logV("opening audio device..."); |
| 156 |
ai=SDL_OpenAudioDevice(request.deviceName.empty()?NULL:request.deviceName.c_str(),0,&ac,&ar,0); |
| 157 |
if (ai==0) { |
| 158 |
logE("could not open audio device: %s",SDL_GetError()); |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
const char* backendName=SDL_GetCurrentAudioDriver(); |
| 163 |
|
| 164 |
desc.deviceName=request.deviceName; |
| 165 |
if (backendName==NULL) { |
| 166 |
desc.name=""; |
| 167 |
} else { |
| 168 |
desc.name=backendName; |
| 169 |
} |
| 170 |
desc.rate=ar.freq; |
| 171 |
desc.inChans=0; |
| 172 |
desc.outChans=ar.channels; |
| 173 |
desc.bufsize=ar.samples; |
| 174 |
desc.fragments=1; |
| 175 |
|
| 176 |
logV("got info: %d channels, %d bufsize",desc.outChans,desc.bufsize); |
| 177 |
|
| 178 |
if (desc.outChans>0) { |
| 179 |
outBufs=new float*[desc.outChans]; |
| 180 |
for (int i=0; i<desc.outChans; i++) { |
| 181 |
outBufs[i]=new float[desc.bufsize]; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
response=desc; |
| 186 |
initialized=true; |
| 187 |
return true; |
| 188 |
} |