blob: acaa6e9b0b27cd97760ada6b1808507f152e26d6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/*
* ADLMIDI Player is a free MIDI player based on a libADLMIDI,
* a Software MIDI synthesizer library with OPL3 emulation
*
* Original ADLMIDI code: Copyright (c) 2010-2014 Joel Yliluoma <bisqwit@iki.fi>
* ADLMIDI Library API: Copyright (c) 2015-2025 Vitaly Novichkov <admin@wohlnet.ru>
*
* Library is based on the ADLMIDI, a MIDI player for Linux and Windows with OPL3 emulation:
* http://iki.fi/bisqwit/source/adlmidi.html
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <adlmidi.h>
#include "audio.h"
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
int audio_init(struct AudioOutputSpec *in_spec, struct AudioOutputSpec *out_obtained, AudioOutputCallback callback)
{
SDL_AudioSpec spec;
SDL_AudioSpec obtained;
int ret;
spec.format = AUDIO_S16SYS;
spec.freq = (int)in_spec->freq;
spec.samples = in_spec->samples;
spec.channels = in_spec->channels;
spec.callback = callback;
switch(in_spec->format)
{
case ADLMIDI_SampleType_S8:
spec.format = AUDIO_S8; break;
case ADLMIDI_SampleType_U8:
spec.format = AUDIO_U8; break;
case ADLMIDI_SampleType_S16:
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
spec.format = AUDIO_S16;
#else
spec.format = AUDIO_S16MSB;
#endif
break;
case ADLMIDI_SampleType_U16:
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
spec.format = AUDIO_U16;
#else
spec.format = AUDIO_U16MSB;
#endif
break;
case ADLMIDI_SampleType_S32:
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
spec.format = AUDIO_S32;
#else
spec.format = AUDIO_S32MSB;
#endif
break;
case ADLMIDI_SampleType_F32:
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
spec.format = AUDIO_F32;
#else
spec.format = AUDIO_F32MSB;
#endif
break;
}
ret = SDL_OpenAudio(&spec, &obtained);
out_obtained->channels = obtained.channels;
out_obtained->freq = (unsigned int)obtained.freq;
out_obtained->samples = obtained.samples;
out_obtained->format = in_spec->format;
out_obtained->is_msb = 0;
switch(obtained.format)
{
case AUDIO_S8:
out_obtained->format = ADLMIDI_SampleType_S8; break;
case AUDIO_U8:
out_obtained->format = ADLMIDI_SampleType_U8; break;
case AUDIO_S16MSB:
out_obtained->is_msb = 1;/* fallthrough */
case AUDIO_S16:
out_obtained->format = ADLMIDI_SampleType_S16; break;
case AUDIO_U16MSB:
out_obtained->is_msb = 1;/* fallthrough */
case AUDIO_U16:
out_obtained->format = ADLMIDI_SampleType_U16; break;
case AUDIO_S32MSB:
out_obtained->is_msb = 1;/* fallthrough */
case AUDIO_S32:
out_obtained->format = ADLMIDI_SampleType_S32; break;
case AUDIO_F32MSB:
out_obtained->is_msb = 1;/* fallthrough */
case AUDIO_F32:
out_obtained->format = ADLMIDI_SampleType_F32; break;
}
return ret;
}
void audio_close(void)
{
SDL_CloseAudio();
}
const char* audio_get_error(void)
{
return SDL_GetError();
}
void audio_start(void)
{
SDL_PauseAudio(0);
}
void audio_stop(void)
{
SDL_PauseAudio(1);
}
void audio_lock(void)
{
SDL_LockAudio();
}
void audio_unlock(void)
{
SDL_UnlockAudio();
}
void audio_delay(unsigned int ms)
{
SDL_Delay(ms);
}
void* audio_mutex_create(void)
{
return SDL_CreateMutex();
}
void audio_mutex_destroy(void *m)
{
SDL_mutex *mut = (SDL_mutex *)m;
SDL_DestroyMutex(mut);
}
void audio_mutex_lock(void *m)
{
SDL_mutex *mut = (SDL_mutex *)m;
SDL_mutexP(mut);
}
void audio_mutex_unlock(void *m)
{
SDL_mutex *mut = (SDL_mutex *)m;
SDL_mutexV(mut);
}
|