MCPcopy Create free account
hub / github.com/JACoders/OpenJK / MP3Stream_InitFromFile

Function MP3Stream_InitFromFile

code/client/cl_mp3.cpp:256–327  ·  view source on GitHub ↗

a file has been loaded in memory, see if we want to keep it as MP3, else as normal WAV... return = qtrue if keeping as MP3 (note: the reason I pass in the unpacked size rather than working it out here is simply because I already have it)

Source from the content-addressed store, hash-verified

254// (note: the reason I pass in the unpacked size rather than working it out here is simply because I already have it)
255//
256qboolean MP3Stream_InitFromFile( sfx_t* sfx, byte *pbSrcData, int iSrcDatalen, const char *psSrcDataFilename,
257 int iMP3UnPackedSize, qboolean bStereoDesired /* = qfalse */
258 )
259{
260 // first, make a decision based on size here as to whether or not it's worth it because of MP3 buffer space
261 // making small files much bigger (and therefore best left as WAV)...
262 //
263
264 if (cv_MP3overhead &&
265 (
266 //iSrcDatalen + sizeof(MP3STREAM) + FUZZY_AMOUNT < iMP3UnPackedSize
267 iSrcDatalen + cv_MP3overhead->integer < iMP3UnPackedSize
268 )
269 )
270 {
271 // ok, let's keep it as MP3 then...
272 //
273 float fMaxVol = 128; // seems to be a reasonable typical default for maxvol (for lip synch). Naturally there's no #define I can use instead...
274
275 MP3_ReadSpecialTagInfo(pbSrcData, iSrcDatalen, NULL, NULL, &fMaxVol ); // try and read a read maxvol from MP3 header
276
277 // fill in some sfx_t fields...
278 //
279// Q_strncpyz( sfx->name, psSrcDataFilename, sizeof(sfx->name) );
280 sfx->eSoundCompressionMethod = ct_MP3;
281 sfx->fVolRange = fMaxVol;
282 //sfx->width = 2;
283 sfx->iSoundLengthInSamples = ((iMP3UnPackedSize / 2/*sfx->width*/) / (44100 / dma.speed)) / (bStereoDesired?2:1);
284 //
285 // alloc mem for data and store it (raw MP3 in this case)...
286 //
287 sfx->pSoundData = (short *) SND_malloc( iSrcDatalen, sfx );
288 memcpy( sfx->pSoundData, pbSrcData, iSrcDatalen );
289
290 // now init the low-level MP3 stuff...
291 //
292 MP3STREAM SFX_MP3Stream = {}; // important to init to all zeroes!
293 char *psError = C_MP3Stream_DecodeInit( &SFX_MP3Stream, /*sfx->data*/ /*sfx->soundData*/ pbSrcData, iSrcDatalen,
294 dma.speed,//(s_khz->value == 44)?44100:(s_khz->value == 22)?22050:11025,
295 2/*sfx->width*/ * 8,
296 bStereoDesired
297 );
298 SFX_MP3Stream.pbSourceData = (byte *) sfx->pSoundData;
299 if (psError)
300 {
301 // This should never happen, since any errors or problems with the MP3 file would have stopped us getting
302 // to this whole function, but just in case...
303 //
304 Com_Printf(va(S_COLOR_YELLOW"File \"%s\": %s\n",psSrcDataFilename,psError));
305
306 // This will leave iSrcDatalen bytes on the hunk stack (since you can't dealloc that), but MP3 files are
307 // usually small, and like I say, it should never happen.
308 //
309 // Strictly speaking, I should do a Z_Malloc above, then I could do a Z_Free if failed, else do a Hunk_Alloc
310 // to copy the Z_Malloc data into, then Z_Free, but for something that shouldn't happen it seemed bad to
311 // penalise the rest of the game with extra alloc demands.
312 //
313 return qfalse;

Callers 1

S_LoadSound_ActualFunction · 0.70

Calls 6

MP3_ReadSpecialTagInfoFunction · 0.70
SND_mallocFunction · 0.70
C_MP3Stream_DecodeInitFunction · 0.50
Com_PrintfFunction · 0.50
vaFunction · 0.50
Z_MallocFunction · 0.50

Tested by

no test coverage detected