| 1 |
#include "SdlAudioRenderer.h" |
| 2 |
|
| 3 |
#include "avifile.h" |
| 4 |
#include "avm_cpuinfo.h" |
| 5 |
#include "avm_output.h" |
| 6 |
|
| 7 |
#include <SDL.h> |
| 8 |
#include <SDL_audio.h> |
| 9 |
|
| 10 |
#include <string.h> |
| 11 |
#include <stdlib.h> |
| 12 |
|
| 13 |
#ifndef SDL_VERSIONNUM |
| 14 |
#define SDL_VERSIONNUM(X, Y, Z) (X)*1000 + (Y)*100 + (Z) |
| 15 |
#endif |
| 16 |
|
| 17 |
#define _SDL_VER SDL_VERSIONNUM(SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL) |
| 18 |
|
| 19 |
AVM_BEGIN_NAMESPACE; |
| 20 |
|
| 21 |
class SdlAudioMix : public IAudioMix |
| 22 |
{ |
| 23 |
int m_iVolume; |
| 24 |
public: |
| 25 |
SdlAudioMix() { SetVolume(); } |
| 26 |
virtual ssize_t Mix(void* data, const void* src, size_t n) const |
| 27 |
{ |
| 28 |
// SDL should use const pointer for src |
| 29 |
if (m_iVolume != SDL_MIX_MAXVOLUME) |
| 30 |
SDL_MixAudio((Uint8*) data, (Uint8*) src, (Uint32) n, m_iVolume); |
| 31 |
else |
| 32 |
memcpy(data, src, n); |
| 33 |
|
| 34 |
return n; |
| 35 |
} |
| 36 |
void SetVolume(int volume = SDL_MIX_MAXVOLUME) { m_iVolume = volume; } |
| 37 |
}; |
| 38 |
|
| 39 |
int SdlAudioRenderer::Init() |
| 40 |
{ |
| 41 |
for (;;) |
| 42 |
{ |
| 43 |
sdl_systems = 0; |
| 44 |
#if _SDL_VER < 1103 |
| 45 |
SDL_Init(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE); |
| 46 |
#else |
| 47 |
Uint32 subsystem_init = SDL_WasInit(SDL_INIT_EVERYTHING); |
| 48 |
|
| 49 |
if (subsystem_init == 0) |
| 50 |
{ |
| 51 |
SDL_Init(SDL_INIT_NOPARACHUTE); |
| 52 |
atexit(SDL_Quit); |
| 53 |
} |
| 54 |
if (!(subsystem_init & SDL_INIT_AUDIO)) |
| 55 |
{ |
| 56 |
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) |
| 57 |
{ |
| 58 |
AVM_WRITE("SDL audio renderer", "failed to init SDL_AUDIO!\n"); |
| 59 |
break; |
| 60 |
} |
| 61 |
sdl_systems |= SDL_INIT_AUDIO; |
| 62 |
} |
| 63 |
#endif |
| 64 |
if (m_Owf.wFormatTag != 0x01) |
| 65 |
{ |
| 66 |
AVM_WRITE("SDL audio renderer", "unsupported audio format 0x%x!\n", m_Owf.wFormatTag); |
| 67 |
break; |
| 68 |
} |
| 69 |
|
| 70 |
SDL_AudioSpec spec; |
| 71 |
SDL_AudioSpec m_Spec; |
| 72 |
memset(&spec, 0, sizeof(spec)); // keep quiet memory validator |
| 73 |
spec.freq = (m_uiUseFreq) ? m_uiUseFreq : m_Owf.nSamplesPerSec; |
| 74 |
spec.format = (m_Owf.wBitsPerSample == 16) ? (uint16_t)AUDIO_S16 : (uint16_t)AUDIO_U8; |
| 75 |
spec.channels = (uint8_t) m_Owf.nChannels; |
| 76 |
spec.samples = 2048; // 1024 causes drops on busy system |
| 77 |
// if (m_Owf.nChannels == 1) spec.samples *= 2; |
| 78 |
// shorter buffer generates more interrupts |
| 79 |
// and would give us more precise timing - |
| 80 |
// on the other hand it's more sensitive to the system load |
| 81 |
// for slowly moving sound - shorter buffer is better |
| 82 |
spec.callback = fillAudio; |
| 83 |
spec.userdata = this; |
| 84 |
|
| 85 |
//printf("SDL freq:%d fmt:0x%x ch:%d s:%d\n", spec.freq, spec.format, spec.channels, spec.samples); |
| 86 |
// to simulate broken audio card |
| 87 |
// spec.freq += -1000; |
| 88 |
if (SDL_OpenAudio(&spec, &m_Spec) < 0) |
| 89 |
{ |
| 90 |
SDL_CloseAudio(); |
| 91 |
AVM_WRITE("SDL audio renderer", "%s!\n", SDL_GetError()); |
| 92 |
break; |
| 93 |
} |
| 94 |
|
| 95 |
AVM_WRITE("SDL audio renderer", 1, "error: %s\n", SDL_GetError()); |
| 96 |
m_dSpecTime = m_Spec.size/(double) m_pQueue->GetBytesPerSec(); |
| 97 |
m_pAudiomix = new SdlAudioMix(); |
| 98 |
AVM_WRITE("SDL audio renderer", 0, "buffer size: %d %dHz\n", m_Spec.size, m_Spec.freq); |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
return -1; |
| 103 |
} |
| 104 |
|
| 105 |
SdlAudioRenderer::~SdlAudioRenderer() |
| 106 |
{ |
| 107 |
AVM_WRITE("SDL audio renderer", 2, "destroy()\n"); |
| 108 |
m_pQueue->Lock(); |
| 109 |
m_bQuit = true; |
| 110 |
m_bInitialized = false; |
| 111 |
m_pQueue->Unlock(); |
| 112 |
m_pQueue->Broadcast(); |
| 113 |
delete m_pAudiomix; |
| 114 |
SDL_CloseAudio(); |
| 115 |
#if _SDL_VER > 1102 |
| 116 |
//we can't call SDL_Quit() for older SDL - causes crash |
| 117 |
if (sdl_systems & SDL_INIT_AUDIO) |
| 118 |
SDL_QuitSubSystem(sdl_systems); |
| 119 |
#endif |
| 120 |
AVM_WRITE("SDL audio renderer", 2, "destroy() successful\n"); |
| 121 |
} |
| 122 |
|
| 123 |
int SdlAudioRenderer::SetVolume(int volume) |
| 124 |
{ |
| 125 |
int hr = IAudioRenderer::SetVolume(volume); |
| 126 |
if (hr == 0) |
| 127 |
m_pAudiomix->SetVolume(volume * SDL_MIX_MAXVOLUME / VOL_MAX); |
| 128 |
return hr; |
| 129 |
} |
| 130 |
|
| 131 |
void SdlAudioRenderer::fillAudio(void* userdata, unsigned char* stream, int len) |
| 132 |
{ |
| 133 |
//AVM_WRITE("SDL audio renderer", 3, "fillAudio()\n"); |
| 134 |
SdlAudioRenderer& a = *(SdlAudioRenderer*)userdata; |
| 135 |
a.m_pQueue->Lock(); |
| 136 |
while (!a.m_bQuit && a.m_bInitialized) |
| 137 |
{ |
| 138 |
if (a.m_pQueue->GetSize() < unsigned(len)) |
| 139 |
{ |
| 140 |
//printf("EOF: %d\n", a.m_pAudiostream->Eof()); |
| 141 |
memset(stream, 0, len); |
| 142 |
if (a.m_pAudiostream->Eof()) |
| 143 |
{ |
| 144 |
// play the rest |
| 145 |
len = (int) a.m_pQueue->GetSize(); |
| 146 |
if (len == 0) |
| 147 |
break; |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
// wait for buffer being filled |
| 152 |
a.m_pQueue->Wait(0.01f); |
| 153 |
continue; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
#if 0 |
| 158 |
int64_t ct = avm_get_time_us(); |
| 159 |
static int64_t las = ct; |
| 160 |
AVM_WRITE("SDL audio renderer", "diff %f\n", avm_get_time_diff(ct, las)); las = ct; |
| 161 |
#endif |
| 162 |
a.m_pQueue->Read(stream, len, a.m_pAudiomix); |
| 163 |
a.updateTimer(); |
| 164 |
#if 0 |
| 165 |
SDL_AudioSpec spec; |
| 166 |
spec.freq = (a.m_uiUseFreq) ? a.m_uiUseFreq : a.m_Owf.nSamplesPerSec; |
| 167 |
spec.format = (a.m_Owf.wBitsPerSample == 16) ? AUDIO_S16 : AUDIO_U8; |
| 168 |
spec.channels = a.m_Owf.nChannels; |
| 169 |
AVM_WRITE("SDL audio renderer", "OLDAUDIO freq:%d format:%d channels:%d\n", |
| 170 |
a.m_Spec.freq, a.m_Spec.format, a.m_Spec.channels); |
| 171 |
|
| 172 |
AVM_WRITE("SDL audio renderer", "NEWAUDIO freq:%d format:%d channels:%d\n", |
| 173 |
spec.freq, spec.format, spec.channels); |
| 174 |
#endif |
| 175 |
break; |
| 176 |
} |
| 177 |
a.m_pQueue->Unlock(); |
| 178 |
} |
| 179 |
|
| 180 |
void SdlAudioRenderer::pause(int v) |
| 181 |
{ |
| 182 |
SDL_PauseAudio(v); |
| 183 |
} |
| 184 |
|
| 185 |
#undef _SDL_VER |
| 186 |
|
| 187 |
AVM_END_NAMESPACE; |