gzdoom/src/sound/i_sound.cpp

602 lines
14 KiB
C++
Raw Normal View History

/*
** i_sound.cpp
** Stubs for sound interfaces.
**
**---------------------------------------------------------------------------
** Copyright 1998-2006 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#include "resource.h"
extern HWND Window;
extern HINSTANCE g_hInst;
#define USE_WINDOWS_DWORD
#else
#define FALSE 0
#define TRUE 1
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <memory>
#include "doomtype.h"
#include <math.h>
#include "fmodsound.h"
#include "oalsound.h"
#include "m_swap.h"
#include "stats.h"
#include "files.h"
#include "c_cvars.h"
#include "c_dispatch.h"
#include "i_system.h"
#include "i_sound.h"
#include "i_music.h"
#include "m_argv.h"
#include "m_misc.h"
#include "w_wad.h"
#include "i_video.h"
#include "s_sound.h"
- The garbage collector is now run one last time just before exiting the game. - Removed movie volume from the sound menu and renamed some of the other options to give the MIDI device name more room to display itself. - Moved the midi device selection into the main sound menu. - Added FMOD as MIDI device -1, to replace the MIDI mapper. This is still the default device. By default, it uses exactly the same DLS instruments as the Microsoft GS Wavetable Synth. If you have another set DLS level 1 patch set you want to use, set the snd_midipatchfile cvar to specify where it should load the instruments from. - Changed the ProduceMIDI function to store its output into a TArray<BYTE>. An overloaded version wraps around it to continue to supply file-writing support for external Timidity++ usage. - Added an FMOD credits banner to comply with their non-commercial license. - Reimplemented the snd_buffersize cvar for the FMOD Ex sound system. Rather than a time in ms, this is now the length in samples of the DSP buffer. Also added the snd_buffercount cvar to offer complete control over the call to FMOD::System::setDSPBufferSize(). Note that with any snd_samplerate below about 44kHz, you will need to set snd_buffersize to avoid long latencies. - Reimplemented the snd_output cvar for the FMOD Ex sound system. - Changed snd_samplerate default to 0. This now means to use the default sample rate. - Made snd_output, snd_output_format, snd_speakermode, snd_resampler, and snd_hrtf available through the menu. - Split the HRTF effect selection into its own cvar: snd_hrtf. - Removed 96000 Hz option from the menu. It's still available through the cvar, if desired. - Fixed: If Windows sound init failed, retry with DirectSound. (Apparently, WASAPI doesn't work with more than two speakers and PCM-Float output at the same time.) - Fixed: Area sounds only played from the front speakers once you got within the 2D panning area. SVN r854 (trunk)
2008-03-26 04:27:07 +00:00
#include "v_text.h"
#include "gi.h"
#include "doomdef.h"
EXTERN_CVAR (Float, snd_sfxvolume)
- The garbage collector is now run one last time just before exiting the game. - Removed movie volume from the sound menu and renamed some of the other options to give the MIDI device name more room to display itself. - Moved the midi device selection into the main sound menu. - Added FMOD as MIDI device -1, to replace the MIDI mapper. This is still the default device. By default, it uses exactly the same DLS instruments as the Microsoft GS Wavetable Synth. If you have another set DLS level 1 patch set you want to use, set the snd_midipatchfile cvar to specify where it should load the instruments from. - Changed the ProduceMIDI function to store its output into a TArray<BYTE>. An overloaded version wraps around it to continue to supply file-writing support for external Timidity++ usage. - Added an FMOD credits banner to comply with their non-commercial license. - Reimplemented the snd_buffersize cvar for the FMOD Ex sound system. Rather than a time in ms, this is now the length in samples of the DSP buffer. Also added the snd_buffercount cvar to offer complete control over the call to FMOD::System::setDSPBufferSize(). Note that with any snd_samplerate below about 44kHz, you will need to set snd_buffersize to avoid long latencies. - Reimplemented the snd_output cvar for the FMOD Ex sound system. - Changed snd_samplerate default to 0. This now means to use the default sample rate. - Made snd_output, snd_output_format, snd_speakermode, snd_resampler, and snd_hrtf available through the menu. - Split the HRTF effect selection into its own cvar: snd_hrtf. - Removed 96000 Hz option from the menu. It's still available through the cvar, if desired. - Fixed: If Windows sound init failed, retry with DirectSound. (Apparently, WASAPI doesn't work with more than two speakers and PCM-Float output at the same time.) - Fixed: Area sounds only played from the front speakers once you got within the 2D panning area. SVN r854 (trunk)
2008-03-26 04:27:07 +00:00
CVAR (Int, snd_samplerate, 0, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
CVAR (Int, snd_buffersize, 0, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
CVAR (String, snd_output, "default", CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
#ifndef NO_FMOD
CVAR (String, snd_backend, "fmod", CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
#elif !defined(NO_OPENAL)
CVAR (String, snd_backend, "openal", CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
#else
CVAR (String, snd_backend, "null", CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
#endif
// killough 2/21/98: optionally use varying pitched sounds
CVAR (Bool, snd_pitched, false, CVAR_ARCHIVE)
SoundRenderer *GSnd;
bool nosound;
bool nosfx;
void I_CloseSound ();
//
// SFX API
//
//==========================================================================
//
// CVAR snd_sfxvolume
//
// Maximum volume of a sound effect.
//==========================================================================
CUSTOM_CVAR (Float, snd_sfxvolume, 1.f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG|CVAR_NOINITCALL)
{
if (self < 0.f)
self = 0.f;
else if (self > 1.f)
self = 1.f;
else if (GSnd != NULL)
{
GSnd->SetSfxVolume (self);
}
}
class NullSoundRenderer : public SoundRenderer
{
public:
virtual bool IsNull() { return true; }
void SetSfxVolume (float volume)
{
}
void SetMusicVolume (float volume)
{
}
SoundHandle LoadSound(BYTE *sfxdata, int length)
{
SoundHandle retval = { NULL };
return retval;
}
SoundHandle LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend)
{
SoundHandle retval = { NULL };
return retval;
}
void UnloadSound (SoundHandle sfx)
{
}
unsigned int GetMSLength(SoundHandle sfx)
{
// Return something that isn't 0. This is only used by some
// ambient sounds to specify a default minimum period.
return 250;
}
unsigned int GetSampleLength(SoundHandle sfx)
{
return 0;
}
float GetOutputRate()
{
return 11025; // Lies!
}
void StopChannel(FISoundChannel *chan)
{
}
void ChannelVolume(FISoundChannel *, float)
{
}
// Streaming sounds.
SoundStream *CreateStream (SoundStreamCallback callback, int buffbytes, int flags, int samplerate, void *userdata)
{
return NULL;
}
SoundStream *OpenStream (const char *filename, int flags, int offset, int length)
{
return NULL;
}
// Starts a sound.
FISoundChannel *StartSound (SoundHandle sfx, float vol, int pitch, int chanflags, FISoundChannel *reuse_chan)
{
return NULL;
}
FISoundChannel *StartSound3D (SoundHandle sfx, SoundListener *listener, float vol, FRolloffInfo *rolloff, float distscale, int pitch, int priority, const FVector3 &pos, const FVector3 &vel, int channum, int chanflags, FISoundChannel *reuse_chan)
{
return NULL;
}
// Marks a channel's start time without actually playing it.
void MarkStartTime (FISoundChannel *chan)
{
}
// Returns position of sound on this channel, in samples.
unsigned int GetPosition(FISoundChannel *chan)
{
return 0;
}
// Gets a channel's audibility (real volume).
float GetAudibility(FISoundChannel *chan)
{
return 0;
}
// Synchronizes following sound startups.
void Sync (bool sync)
{
}
// Pauses or resumes all sound effect channels.
void SetSfxPaused (bool paused, int slot)
{
}
// Pauses or resumes *every* channel, including environmental reverb.
void SetInactive(SoundRenderer::EInactiveState inactive)
{
}
// Updates the volume, separation, and pitch of a sound channel.
void UpdateSoundParams3D (SoundListener *listener, FISoundChannel *chan, bool areasound, const FVector3 &pos, const FVector3 &vel)
{
}
void UpdateListener (SoundListener *)
{
}
void UpdateSounds ()
{
}
bool IsValid ()
{
return true;
}
void PrintStatus ()
{
Printf("Null sound module active.\n");
}
void PrintDriversList ()
{
Printf("Null sound module uses no drivers.\n");
}
FString GatherStats ()
{
return "Null sound module has no stats.";
}
};
void I_InitSound ()
{
/* Get command line options: */
nosound = !!Args->CheckParm ("-nosound");
nosfx = !!Args->CheckParm ("-nosfx");
if (nosound)
{
GSnd = new NullSoundRenderer;
I_InitMusic ();
return;
}
if(stricmp(snd_backend, "null") == 0)
GSnd = new NullSoundRenderer;
#ifndef NO_FMOD
else if(stricmp(snd_backend, "fmod") == 0)
GSnd = new FMODSoundRenderer;
#endif
#ifndef NO_OPENAL
else if(stricmp(snd_backend, "openal") == 0)
GSnd = new OpenALSoundRenderer;
#endif
if (!GSnd || !GSnd->IsValid ())
{
I_CloseSound();
GSnd = new NullSoundRenderer;
- The garbage collector is now run one last time just before exiting the game. - Removed movie volume from the sound menu and renamed some of the other options to give the MIDI device name more room to display itself. - Moved the midi device selection into the main sound menu. - Added FMOD as MIDI device -1, to replace the MIDI mapper. This is still the default device. By default, it uses exactly the same DLS instruments as the Microsoft GS Wavetable Synth. If you have another set DLS level 1 patch set you want to use, set the snd_midipatchfile cvar to specify where it should load the instruments from. - Changed the ProduceMIDI function to store its output into a TArray<BYTE>. An overloaded version wraps around it to continue to supply file-writing support for external Timidity++ usage. - Added an FMOD credits banner to comply with their non-commercial license. - Reimplemented the snd_buffersize cvar for the FMOD Ex sound system. Rather than a time in ms, this is now the length in samples of the DSP buffer. Also added the snd_buffercount cvar to offer complete control over the call to FMOD::System::setDSPBufferSize(). Note that with any snd_samplerate below about 44kHz, you will need to set snd_buffersize to avoid long latencies. - Reimplemented the snd_output cvar for the FMOD Ex sound system. - Changed snd_samplerate default to 0. This now means to use the default sample rate. - Made snd_output, snd_output_format, snd_speakermode, snd_resampler, and snd_hrtf available through the menu. - Split the HRTF effect selection into its own cvar: snd_hrtf. - Removed 96000 Hz option from the menu. It's still available through the cvar, if desired. - Fixed: If Windows sound init failed, retry with DirectSound. (Apparently, WASAPI doesn't work with more than two speakers and PCM-Float output at the same time.) - Fixed: Area sounds only played from the front speakers once you got within the 2D panning area. SVN r854 (trunk)
2008-03-26 04:27:07 +00:00
Printf (TEXTCOLOR_RED"Sound init failed. Using nosound.\n");
}
I_InitMusic ();
snd_sfxvolume.Callback ();
}
void I_CloseSound ()
{
// Free all loaded samples
for (unsigned i = 0; i < S_sfx.Size(); i++)
{
S_UnloadSound(&S_sfx[i]);
}
delete GSnd;
GSnd = NULL;
}
void I_ShutdownSound()
{
if (GSnd != NULL)
{
S_StopAllChannels();
I_CloseSound();
}
}
CCMD (snd_status)
{
GSnd->PrintStatus ();
}
CCMD (snd_reset)
{
- The sound code now handles restarting looping sounds itself. As far as the rest of the game is concerned, these sounds will never stop once they have been started until they are explicitly stopped. If they are evicted from their channels, the sound code will restart them as soon as possible. This means that instead of this: if (!S_IsActorPlayingSomething(actor, CHAN_WEAPON, -1)) { S_Sound(actor, CHAN_WEAPON|CHAN_LOOP, soundid, 1, ATTN_NORM); } The following is now just as effective: S_Sound(actor, CHAN_WEAPON|CHAN_LOOP, soundid, 1, ATTN_NORM); There are also a couple of other ramifications presented by this change: * The full state of the sound system (sans music) is now stored in save games. Any sounds that were playing when you saved will still be playing when you load. (Try saving while Korax is making a speech in Hexen to hear it.) * Using snd_reset will also preserve any playing sounds. * Movie playback is disabled, probably forever. I did not want to update the MovieDisable/ResumeSound stuff for the new eviction tracking code. A properly updated movie player will use the VMR, which doesn't need these functions, since it would pipe the sound straight through the sound system like everything else, so I decided to dump them now, which leaves the movie player in a totally unworkable state. June 26, 2008 - Changed S_Sound() to take the same floating point attenuation that the internal S_StartSound() uses. Now ambient sounds can use the public S_Sound() interface. - Fixed: S_RelinkSound() compared the points of the channels against the from actor's point, rather than checking the channels' mover. - Changed Strife's animated doors so that their sounds originate from the interior of the sector making them and not from the entire vertical height of the map. SVN r1055 (trunk)
2008-06-29 04:19:38 +00:00
I_ShutdownMusic();
S_EvictAllChannels();
I_CloseSound();
- The sound code now handles restarting looping sounds itself. As far as the rest of the game is concerned, these sounds will never stop once they have been started until they are explicitly stopped. If they are evicted from their channels, the sound code will restart them as soon as possible. This means that instead of this: if (!S_IsActorPlayingSomething(actor, CHAN_WEAPON, -1)) { S_Sound(actor, CHAN_WEAPON|CHAN_LOOP, soundid, 1, ATTN_NORM); } The following is now just as effective: S_Sound(actor, CHAN_WEAPON|CHAN_LOOP, soundid, 1, ATTN_NORM); There are also a couple of other ramifications presented by this change: * The full state of the sound system (sans music) is now stored in save games. Any sounds that were playing when you saved will still be playing when you load. (Try saving while Korax is making a speech in Hexen to hear it.) * Using snd_reset will also preserve any playing sounds. * Movie playback is disabled, probably forever. I did not want to update the MovieDisable/ResumeSound stuff for the new eviction tracking code. A properly updated movie player will use the VMR, which doesn't need these functions, since it would pipe the sound straight through the sound system like everything else, so I decided to dump them now, which leaves the movie player in a totally unworkable state. June 26, 2008 - Changed S_Sound() to take the same floating point attenuation that the internal S_StartSound() uses. Now ambient sounds can use the public S_Sound() interface. - Fixed: S_RelinkSound() compared the points of the channels against the from actor's point, rather than checking the channels' mover. - Changed Strife's animated doors so that their sounds originate from the interior of the sector making them and not from the entire vertical height of the map. SVN r1055 (trunk)
2008-06-29 04:19:38 +00:00
I_InitSound();
S_RestartMusic();
S_RestoreEvictedChannels();
}
CCMD (snd_listdrivers)
{
GSnd->PrintDriversList ();
}
ADD_STAT (sound)
{
return GSnd->GatherStats ();
}
SoundRenderer::SoundRenderer ()
{
}
SoundRenderer::~SoundRenderer ()
{
}
FString SoundRenderer::GatherStats ()
{
return "No stats for this sound renderer.";
}
short *SoundRenderer::DecodeSample(int outlen, const void *coded, int sizebytes, ECodecType ctype)
{
short *samples = (short*)calloc(1, outlen);
ChannelConfig chans;
SampleType type;
int srate;
std::auto_ptr<SoundDecoder> decoder(CreateDecoder((const BYTE*)coded, sizebytes));
if(!decoder.get()) return samples;
decoder->getInfo(&srate, &chans, &type);
if(chans != ChannelConfig_Mono || type != SampleType_Int16)
{
DPrintf("Sample is not 16-bit mono\n");
return samples;
}
decoder->read((char*)samples, outlen);
return samples;
}
void SoundRenderer::DrawWaveDebug(int mode)
{
}
SoundStream::~SoundStream ()
{
}
bool SoundStream::SetPosition(unsigned int pos)
{
return false;
}
bool SoundStream::SetOrder(int order)
{
VERY IMPORTANT NOTE FOR ANYBODY BUILDING FROM THE TRUNK: This commit adds support for FMOD Ex while at the same time removing support for FMOD 3. Be sure to update your SDKs. GCC users, be sure to do a "make cleandep && make clean" before building, or you will likely get inexplicable errors. - Fixed: If you wanted to make cleandep with MinGW, you had to specifically specify Makefile.mingw as the makefile to use. - Added a normalizer to the OPL synth. It helped bring up the volume a little, but not nearly as much as I would have liked. - Removed MIDI Mapper references. It doesn't work with the stream API, and it doesn't really exist on NT kernels, either. - Reworked music volume: Except for MIDI, all music volume is controlled through GSnd and not at the individual song level. - Removed the mididevice global variable. - Removed snd_midivolume. Now that all music uses a linear volume scale, there's no need for two separate music volume controls. - Increased snd_samplerate default up to 48000. - Added snd_format, defaulting to "PCM-16". - Added snd_speakermode, defaulting to "Auto". - Replaced snd_fpu with snd_resampler, defaulting to "Linear". - Bumped the snd_channels default up from a pitiful 12 to 32. - Changed snd_3d default to true. The new cvar snd_hw3d determines if hardware 3D support is used and default to false. - Removed the libFLAC source, since FMOD Ex has native FLAC support. - Removed the altsound code, since it was terribly gimped in comparison to the FMOD code. It's original purpose was to have been as a springboard for writing a non-FMOD sound system for Unix-y systems, but that never happened. - Finished preliminary FMOD Ex support. SVN r789 (trunk)
2008-03-09 03:13:49 +00:00
return false;
}
- The garbage collector is now run one last time just before exiting the game. - Removed movie volume from the sound menu and renamed some of the other options to give the MIDI device name more room to display itself. - Moved the midi device selection into the main sound menu. - Added FMOD as MIDI device -1, to replace the MIDI mapper. This is still the default device. By default, it uses exactly the same DLS instruments as the Microsoft GS Wavetable Synth. If you have another set DLS level 1 patch set you want to use, set the snd_midipatchfile cvar to specify where it should load the instruments from. - Changed the ProduceMIDI function to store its output into a TArray<BYTE>. An overloaded version wraps around it to continue to supply file-writing support for external Timidity++ usage. - Added an FMOD credits banner to comply with their non-commercial license. - Reimplemented the snd_buffersize cvar for the FMOD Ex sound system. Rather than a time in ms, this is now the length in samples of the DSP buffer. Also added the snd_buffercount cvar to offer complete control over the call to FMOD::System::setDSPBufferSize(). Note that with any snd_samplerate below about 44kHz, you will need to set snd_buffersize to avoid long latencies. - Reimplemented the snd_output cvar for the FMOD Ex sound system. - Changed snd_samplerate default to 0. This now means to use the default sample rate. - Made snd_output, snd_output_format, snd_speakermode, snd_resampler, and snd_hrtf available through the menu. - Split the HRTF effect selection into its own cvar: snd_hrtf. - Removed 96000 Hz option from the menu. It's still available through the cvar, if desired. - Fixed: If Windows sound init failed, retry with DirectSound. (Apparently, WASAPI doesn't work with more than two speakers and PCM-Float output at the same time.) - Fixed: Area sounds only played from the front speakers once you got within the 2D panning area. SVN r854 (trunk)
2008-03-26 04:27:07 +00:00
FString SoundStream::GetStats()
{
return "No stream stats available.";
}
//==========================================================================
//
// SoundRenderer :: LoadSoundVoc
//
//==========================================================================
SoundHandle SoundRenderer::LoadSoundVoc(BYTE *sfxdata, int length)
{
BYTE * data = NULL;
int len, frequency, channels, bits, loopstart, loopend;
len = frequency = channels = bits = 0;
loopstart = loopend = -1;
do if (length > 26)
{
// First pass to parse data and validate the file
if (strncmp ((const char *)sfxdata, "Creative Voice File", 19))
break;
int i = 26, blocktype = 0, blocksize = 0, codec = -1;
bool noextra = true, okay = true;
while (i < length)
{
// Read block header
blocktype = sfxdata[i];
if (blocktype == 0)
break;
blocksize = sfxdata[i+1] + (sfxdata[i+2]<<8) + (sfxdata[i+3]<<16);
i += 4;
if (i + blocksize > length)
{
okay = false;
break;
}
// Read block data
switch (blocktype)
{
case 1: // Sound data
if (noextra && (codec == -1 || codec == sfxdata[i+1]))
{
frequency = 1000000/(256 - sfxdata[i]);
channels = 1;
codec = sfxdata[i+1];
if (codec == 0)
bits = 8;
else if (codec == 4)
bits = -16;
else okay = false;
len += blocksize - 2;
}
break;
case 2: // Sound data continuation
if (codec == -1)
okay = false;
len += blocksize;
break;
case 3: // Silence
if (frequency == 1000000/(256 - sfxdata[i+2]))
{
int silength = 1 + sfxdata[i] + (sfxdata[i+1]<<8);
if (codec == 0) // 8-bit unsigned PCM
len += silength;
else if (codec == 4) // 16-bit signed PCM
len += silength<<1;
else okay = false;
} else okay = false;
break;
case 4: // Mark (ignored)
case 5: // Text (ignored)
break;
case 6: // Repeat start
loopstart = len;
break;
case 7: // Repeat end
loopend = len;
if (loopend < loopstart)
okay = false;
break;
case 8: // Extra info
noextra = false;
if (codec == -1)
{
codec = sfxdata[i+2];
channels = 1+sfxdata[i+3];
frequency = 256000000/(channels * (65536 - (sfxdata[i]+(sfxdata[i+1]<<8))));
} else okay = false;
break;
case 9: // Sound data in new format
if (codec == -1)
{
frequency = sfxdata[i] + (sfxdata[i+1]<<8) + (sfxdata[i+2]<<16) + (sfxdata[i+3]<<24);
bits = sfxdata[i+4];
channels = sfxdata[i+5];
codec = sfxdata[i+6] + (sfxdata[i+7]<<8);
if (codec == 0)
bits = 8;
else if (codec == 4)
bits = -16;
else okay = false;
len += blocksize - 12;
} else okay = false;
break;
default: // Unknown block type
okay = false;
DPrintf ("Unknown VOC block type %i\n", blocktype);
break;
}
// Move to next block
i += blocksize;
}
// Second pass to write the data
if (okay)
{
data = new BYTE[len];
i = 26;
int j = 0;
while (i < length)
{
// Read block header again
blocktype = sfxdata[i];
if (blocktype == 0) break;
blocksize = sfxdata[i+1] + (sfxdata[i+2]<<8) + (sfxdata[i+3]<<16);
i += 4;
switch (blocktype)
{
case 1: memcpy(data+j, sfxdata+i+2, blocksize-2 ); j += blocksize-2; break;
case 2: memcpy(data+j, sfxdata+i, blocksize ); j += blocksize; break;
case 9: memcpy(data+j, sfxdata+i+12, blocksize-12); j += blocksize-12; break;
case 3:
{
int silength = 1 + sfxdata[i] + (sfxdata[i+1]<<8);
if (bits == 8)
{
memset(data+j, 128, silength);
j += silength;
}
else if (bits == -16)
{
memset(data+j, 0, silength<<1);
j += silength<<1;
}
}
break;
default: break;
}
}
}
} while (false);
SoundHandle retval = LoadSoundRaw(data, len, frequency, channels, bits, loopstart, loopend);
if (data) delete[] data;
return retval;
}
2014-06-19 11:13:42 +00:00
SoundDecoder *SoundRenderer::CreateDecoder(const BYTE *sfxdata, int length)
{
2014-06-19 11:13:42 +00:00
SoundDecoder *decoder = NULL;
#ifdef HAVE_MPG123
decoder = new MPG123Decoder;
2014-06-20 05:46:46 +00:00
if(decoder->open((const char*)sfxdata, length))
return decoder;
delete decoder;
decoder = NULL;
#endif
2014-06-19 11:13:42 +00:00
#ifdef HAVE_SNDFILE
2014-06-20 05:46:46 +00:00
decoder = new SndFileDecoder;
if(decoder->open((const char*)sfxdata, length))
return decoder;
delete decoder;
decoder = NULL;
2014-06-19 11:13:42 +00:00
#endif
return decoder;
}
SoundDecoder* SoundRenderer::CreateDecoder(const char *fname, int offset, int length)
{
SoundDecoder *decoder = NULL;
2014-06-20 03:14:16 +00:00
#ifdef HAVE_MPG123
decoder = new MPG123Decoder;
2014-06-20 05:46:46 +00:00
if(decoder->open(fname, offset, length))
return decoder;
delete decoder;
decoder = NULL;
2014-06-20 03:14:16 +00:00
#endif
2014-06-19 11:13:42 +00:00
#ifdef HAVE_SNDFILE
decoder = new SndFileDecoder;
2014-06-20 05:46:46 +00:00
if(decoder->open(fname, offset, length))
return decoder;
delete decoder;
decoder = NULL;
2014-06-19 11:13:42 +00:00
#endif
return decoder;
}
// Default readAll implementation, for decoders that can't do anything better
std::vector<char> SoundDecoder::readAll()
{
std::vector<char> output;
size_t total = 0;
size_t got;
output.resize(total+32768);
while((got=read(&output[total], output.size()-total)) > 0)
{
total += got;
output.resize(total*2);
}
output.resize(total);
return output;
}