/
audiodriver_music.c
237 lines (204 loc) · 6.86 KB
/
audiodriver_music.c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* @file audiodriver_music.c
* Low-level music interface of the audio driver. @ingroup audio
*
* @authors Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2007-2012 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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 2 of the License, or (at your
* option) 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, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "de_base.h"
#include "de_console.h"
#include "de_filesys.h"
#include "audiodriver_music.h"
#define BUFFERED_MUSIC_FILE "dd-buffered-song"
static boolean needBufFileSwitch = false;
static AutoStr* composeBufferedMusicFilename(int id, const char* ext)
{
if(ext && ext[0])
{
return Str_Appendf(AutoStr_New(), "%s%i%s", BUFFERED_MUSIC_FILE, id, ext);
}
else
{
return Str_Appendf(AutoStr_New(), "%s%i", BUFFERED_MUSIC_FILE, id);
}
}
static void musicSet(audiointerface_music_t* iMusic, int property, void* ptr)
{
audiodriver_t* d = AudioDriver_Interface(iMusic);
if(!d || !d->Set) return;
d->Set(property, ptr);
}
static int musicPlayNativeFile(audiointerface_music_t* iMusic, const char* fileName, boolean looped)
{
if(!iMusic->PlayFile) return 0;
return iMusic->PlayFile(fileName, looped);
}
static int musicPlayLump(audiointerface_music_t* iMusic, lumpnum_t lump, boolean looped)
{
abstractfile_t* fsObject;
size_t lumpLength;
int lumpIdx;
fsObject = F_FindFileForLumpNum2(lump, &lumpIdx);
lumpLength = F_LumpLength(lump);
if(!iMusic->Play || !iMusic->SongBuffer)
{
// Music interface does not offer buffer playback.
// Write this lump to disk and play from there.
AutoStr* musicFile = AudioDriver_Music_ComposeTempBufferFilename(0);
if(!F_DumpLump(lump, Str_Text(musicFile)))
{
// Failed to write the lump...
return 0;
}
return musicPlayNativeFile(iMusic, Str_Text(musicFile), looped);
}
// Buffer the data using the driver's facilities.
F_ReadLumpSection(fsObject, lumpIdx,
(uint8_t*) iMusic->SongBuffer(lumpLength),
0, lumpLength);
return iMusic->Play(looped);
}
static int musicPlayFile(audiointerface_music_t* iMusic, const char* virtualOrNativePath, boolean looped)
{
size_t len;
DFile* file = F_Open(virtualOrNativePath, "rb");
if(!file) return 0;
len = DFile_Length(file);
if(!iMusic->Play || !iMusic->SongBuffer)
{
// Music interface does not offer buffer playback.
// Write to disk and play from there.
AutoStr* fileName = AudioDriver_Music_ComposeTempBufferFilename(NULL);
uint8_t* buf = (uint8_t*)malloc(len);
if(!buf)
{
F_Delete(file);
Con_Message("Warning: Failed on allocation of %lu bytes for temporary song write buffer.\n", (unsigned long) len);
return false;
}
DFile_Read(file, buf, len);
F_Dump(buf, len, Str_Text(fileName));
free(buf);
F_Delete(file);
// Music maestro, if you please!
return musicPlayNativeFile(iMusic, Str_Text(fileName), looped);
}
else
{
// Music interface offers buffered playback. Use it.
DFile_Read(file, (uint8_t*) iMusic->SongBuffer(len), len);
F_Delete(file);
return iMusic->Play(looped);
}
}
static int musicPlayCDTrack(audiointerface_cd_t* iCD, int track, boolean looped)
{
return iCD->Play(track, looped);
}
static boolean musicIsPlaying(audiointerface_music_t* iMusic)
{
return iMusic->gen.Get(MUSIP_PLAYING, 0);
}
void AudioDriver_Music_SwitchBufferFilenames(void)
{
needBufFileSwitch = true;
}
AutoStr* AudioDriver_Music_ComposeTempBufferFilename(const char* ext)
{
static int currentBufFile = 0;
// Switch the name of the buffered song file?
if(needBufFileSwitch)
{
currentBufFile ^= 1;
needBufFileSwitch = false;
}
return composeBufferedMusicFilename(currentBufFile, ext);
}
void AudioDriver_Music_Set(int property, void* ptr)
{
void* ifs[MAX_AUDIO_INTERFACES];
int i, count = AudioDriver_FindInterfaces(AUDIO_IMUSIC, ifs);
for(i = 0; i < count; ++i)
{
musicSet((audiointerface_music_t*) ifs[i], property, ptr);
}
if(property == AUDIOP_SOUNDFONT_FILENAME)
{
const char* fn = ptr;
if(!fn || !fn[0]) return; // No path.
if(F_FileExists(fn))
Con_Message("Current soundfont set to: \"%s\"\n", fn);
else
Con_Message("Warning: Soundfont \"%s\" not found.\n", fn);
}
}
int AudioDriver_Music_PlayNativeFile(const char* fileName, boolean looped)
{
void* ifs[MAX_AUDIO_INTERFACES];
int i, count = AudioDriver_FindInterfaces(AUDIO_IMUSIC, ifs);
for(i = 0; i < count; ++i)
{
if(musicPlayNativeFile((audiointerface_music_t*) ifs[i], fileName, looped))
return true;
}
return false;
}
int AudioDriver_Music_PlayLump(lumpnum_t lump, boolean looped)
{
void* ifs[MAX_AUDIO_INTERFACES];
int i, count = AudioDriver_FindInterfaces(AUDIO_IMUSIC, ifs);
for(i = 0; i < count; ++i)
{
if(musicPlayLump((audiointerface_music_t*) ifs[i], lump, looped))
return true;
}
return false;
}
int AudioDriver_Music_PlayFile(const char* virtualOrNativePath, boolean looped)
{
void* ifs[MAX_AUDIO_INTERFACES];
int i, count = AudioDriver_FindInterfaces(AUDIO_IMUSIC, ifs);
for(i = 0; i < count; ++i)
{
if(musicPlayFile((audiointerface_music_t*) ifs[i], virtualOrNativePath, looped))
return true;
}
return false;
}
int AudioDriver_Music_PlayCDTrack(int track, boolean looped)
{
void* ifs[MAX_AUDIO_INTERFACES];
int i, count = AudioDriver_FindInterfaces(AUDIO_ICD, ifs);
for(i = 0; i < count; ++i)
{
if(musicPlayCDTrack((audiointerface_cd_t*) ifs[i], track, looped))
return true;
}
return false;
}
boolean AudioDriver_Music_IsPlaying(void)
{
void* ifs[MAX_AUDIO_INTERFACES];
int i, count = AudioDriver_FindInterfaces(AUDIO_IMUSIC_OR_ICD, ifs);
for(i = 0; i < count; ++i)
{
if(musicIsPlaying((audiointerface_music_t*) ifs[i]))
return true;
}
return false;
}