aboutsummaryrefslogtreecommitdiff
path: root/engines/lab
diff options
context:
space:
mode:
authorFilippos Karapetis2015-02-24 19:22:03 +0200
committerEugene Sandulenko2015-12-15 00:05:02 +0100
commit22b82d7729a2bd40b981a8faba16eff5c708df21 (patch)
treedc4e6c8fe8b0a94b7d84cbe907548509bf3a6093 /engines/lab
parent83e58fe452828f29d59aa1b324d2040b1d6c539b (diff)
downloadscummvm-rg350-22b82d7729a2bd40b981a8faba16eff5c708df21.tar.gz
scummvm-rg350-22b82d7729a2bd40b981a8faba16eff5c708df21.tar.bz2
scummvm-rg350-22b82d7729a2bd40b981a8faba16eff5c708df21.zip
LAB: Initial music implementation
The whole music code is greatly simplified
Diffstat (limited to 'engines/lab')
-rw-r--r--engines/lab/audioi.cpp70
-rw-r--r--engines/lab/labfun.h24
-rw-r--r--engines/lab/labmusic.cpp219
3 files changed, 57 insertions, 256 deletions
diff --git a/engines/lab/audioi.cpp b/engines/lab/audioi.cpp
index c841e53680..10b570e835 100644
--- a/engines/lab/audioi.cpp
+++ b/engines/lab/audioi.cpp
@@ -50,63 +50,61 @@ bool EffectPlaying = false, ContMusic = false, DoMusic = false;
static char *CurMusic, *startMusic;
static uint32 StartMusicLen;
static Audio::SoundHandle g_musicHandle;
+static Audio::SoundHandle g_sfxHandle;
+static Audio::QueuingAudioStream *queuingAudioStream = NULL;
+
+static byte *playBuffer;
void freeAudio() {
if (!DoMusic)
return;
- // TODO
- //SDLWrapAudio();
+ g_lab->_mixer->stopHandle(g_musicHandle);
+ g_lab->_mixer->stopHandle(g_sfxHandle);
+
+ delete queuingAudioStream;
+ queuingAudioStream = NULL;
+ delete[] playBuffer;
}
bool initAudio() {
- if (!DoMusic)
- return true;
-
-// TODO
-#if 0
- // we allocate extra mempory for 16-bit samples
- // TODO: 8-bit mono sample for DOS
- buf1 = malloc(PLAYBUFSIZE);
-
- if (buf1 == NULL)
- return false;
-
- buf2 = malloc(PLAYBUFSIZE);
-
- if (buf2 == NULL)
- return false;
-
- if (!SDLInitAudio())
- return false;
-#endif
-
+ playBuffer = new byte[PLAYBUFSIZE];
return true;
}
-bool musicBufferEmpty(uint16 i) {
- // TODO: Multiple streams
- return !g_lab->_mixer->isSoundHandleActive(g_musicHandle);
+bool musicBufferEmpty() {
+ return !g_lab->_mixer->isSoundHandleActive(g_sfxHandle);
}
+uint16 getPlayingBufferCount() {
+ return (queuingAudioStream) ? queuingAudioStream->numQueuedStreams() : 0;
+}
void playMusicBlock(void *Ptr, uint32 Size, uint16 BufferNum, uint16 SampleSpeed) {
- // TODO
-#if 0
+ bool startMusic = false;
if (SampleSpeed < 4000)
SampleSpeed = 4000;
- tempblock = firstblock;
- tempblock.sel_data = Ptr;
- tempblock.len = Size;
+ if (!queuingAudioStream) {
+ queuingAudioStream = Audio::makeQueuingAudioStream(SampleSpeed, false);
+ startMusic = true;
+ }
- SDLPlayBuffer(BufferNum, &tempblock);
-#endif
+ byte soundFlags = Audio::FLAG_LITTLE_ENDIAN;
+ if (g_lab->getPlatform() == Common::kPlatformWindows)
+ soundFlags |= Audio::FLAG_16BITS;
+ else
+ soundFlags |= Audio::FLAG_UNSIGNED;
+
+ queuingAudioStream->queueBuffer((byte *)Ptr, Size, DisposeAfterUse::YES, soundFlags);
+
+ if (startMusic)
+ g_lab->_mixer->playStream(Audio::Mixer::kMusicSoundType, &g_musicHandle, queuingAudioStream);
}
@@ -119,7 +117,7 @@ void updateSoundBuffers() {
// TODO
// FIXME: Very crude implementation
- if (musicBufferEmpty(0)) {
+ if (musicBufferEmpty()) {
flushAudio();
EffectPlaying = false;
}
@@ -170,7 +168,7 @@ void flushAudio() {
if (!DoMusic)
return;
- g_lab->_mixer->stopHandle(g_musicHandle);
+ g_lab->_mixer->stopHandle(g_sfxHandle);
EffectPlaying = false;
}
@@ -206,7 +204,7 @@ void playSoundEffect(uint16 SampleSpeed, uint16 Volume, uint32 Length, bool flus
soundFlags |= Audio::FLAG_UNSIGNED;
Audio::SeekableAudioStream *audStream = Audio::makeRawStream((const byte *)Data, Length, SampleSpeed, soundFlags, DisposeAfterUse::NO);
- g_lab->_mixer->playStream(Audio::Mixer::kSFXSoundType, &g_musicHandle, audStream);
+ g_lab->_mixer->playStream(Audio::Mixer::kSFXSoundType, &g_sfxHandle, audStream);
updateSoundBuffers();
}
diff --git a/engines/lab/labfun.h b/engines/lab/labfun.h
index cc77a4c3f2..d976feea38 100644
--- a/engines/lab/labfun.h
+++ b/engines/lab/labfun.h
@@ -62,17 +62,12 @@ struct SaveGameHeader {
/*----------------------------*/
bool initAudio();
-
void freeAudio();
-
-bool musicBufferEmpty(uint16 i);
-
+bool musicBufferEmpty();
void playMusicBlock(void *Ptr, uint32 Size, uint16 BufferNum, uint16 SampleSpeed);
-
+uint16 getPlayingBufferCount();
void updateSoundBuffers();
-
void flushAudio();
-
void playSoundEffect(uint16 SampleSpeed, uint16 Volume, uint32 Length, bool flush, void *Data);
@@ -222,9 +217,7 @@ void decrypt(byte *text);
/*----- From LabMusic.c -----*/
/*---------------------------*/
-#define MANYBUFFERS 5L
#define MAXBUFFERS 5L
-#define MINBUFFERS 2L
class Music {
public:
@@ -243,15 +236,13 @@ public:
void pauseBackMusic();
void changeMusic(const char *newmusic);
void resetMusic();
- void fileCheckMusic(uint32 filelength);
bool _winmusic, _doNotFileFlushAudio;
bool _turnMusicOn;
bool _musicOn;
private:
- void fillbuffer(uint16 unit);
- uint16 getManyBuffersLeft();
+ void fillbuffer(byte *musicBuffer);
void startMusic(bool startatbegin);
Common::File *_file;
@@ -259,15 +250,8 @@ private:
bool _musicPaused;
bool _tMusicOn;
- uint32 _tFileLength;
uint32 _tLeftInFile;
-
- uint16 _manyBuffers;
-
- void *_musicBuffer[MAXBUFFERS];
- uint16 _musicFilledTo, _musicPlaying, _musicOnBuffer;
-
- uint32 _filelength, _leftinfile;
+ uint32 _leftinfile;
};
diff --git a/engines/lab/labmusic.cpp b/engines/lab/labmusic.cpp
index 4a427eded0..1f5794b995 100644
--- a/engines/lab/labmusic.cpp
+++ b/engines/lab/labmusic.cpp
@@ -50,16 +50,8 @@ Music::Music() {
_musicPaused = false;
_tMusicOn = false;
- _tFileLength = 0;
_tLeftInFile = 0;
- _manyBuffers = MANYBUFFERS;
-
- _musicFilledTo = 0;
- _musicPlaying = 0;
- _musicOnBuffer = 0;
-
- _filelength = 0;
_leftinfile = 0;
_musicOn = false;
@@ -73,143 +65,52 @@ Music::Music() {
/* it from the Audio device. */
/*****************************************************************************/
void Music::updateMusic() {
- uint16 i;
-
WSDL_ProcessInput(0);
updateMouse();
if (EffectPlaying)
updateSoundBuffers();
- else if (_musicOn) {
- for (i = 0; i < 2; i++) {
- if (musicBufferEmpty(i)) {
- playMusicBlock(_musicBuffer[_musicPlaying], MUSICBUFSIZE, i, SAMPLESPEED);
-
- if (_musicPlaying)
- _musicOnBuffer = _musicPlaying - 1;
- else
- _musicOnBuffer = _manyBuffers - 1;
-
- _musicPlaying++;
-
- if (_musicPlaying >= _manyBuffers)
- _musicPlaying = 0;
- }
- }
+
+ if (_musicOn && getPlayingBufferCount() < MAXBUFFERS) {
+ // NOTE: We need to use malloc(), cause this will be freed with free()
+ // by the music code
+ byte *musicBuffer = (byte *)malloc(MUSICBUFSIZE);
+ fillbuffer(musicBuffer);
+ playMusicBlock(musicBuffer, MUSICBUFSIZE, 0, SAMPLESPEED);
}
}
-void Music::fillbuffer(uint16 unit) {
- return;
-
- warning("STUB: Music::fillbuffer");
- uint32 Size = MUSICBUFSIZE;
- void *ptr = _musicBuffer[unit];
-
- if (Size < _leftinfile) {
- _file->read(ptr, Size);
- _leftinfile -= Size;
+void Music::fillbuffer(byte *musicBuffer) {
+ if (MUSICBUFSIZE < _leftinfile) {
+ _file->read(musicBuffer, MUSICBUFSIZE);
+ _leftinfile -= MUSICBUFSIZE;
} else {
- _file->read(ptr, _leftinfile);
+ _file->read(musicBuffer, _leftinfile);
- memset((char *)ptr + _leftinfile, 0, Size - _leftinfile);
+ memset((char *)musicBuffer + _leftinfile, 0, MUSICBUFSIZE - _leftinfile);
_file->seek(0);
- _leftinfile = _filelength;
+ _leftinfile = _file->size();
}
}
-
-/*****************************************************************************/
-/* Figures out how many *complete* buffers of music left to play. */
-/*****************************************************************************/
-uint16 Music::getManyBuffersLeft() {
- uint16 mp = _musicOnBuffer;
-
- if (mp == _musicFilledTo) /* Already filled */
- return _manyBuffers;
- else if (mp > _musicFilledTo)
- return _manyBuffers - (mp - _musicFilledTo);
- else
- return _musicFilledTo - mp;
-}
-
-
-
/*****************************************************************************/
/* Fills up the buffers that have already been played if necessary; if doit */
/* is set to TRUE then it will fill up all empty buffers. Otherwise, it */
/* Check if there are MINBUFFERS or less buffers that are playing. */
/*****************************************************************************/
void Music::fillUpMusic(bool doit) {
- int16 ManyLeft, ManyFill;
-
updateMusic();
-
- if (!_musicOn)
- return;
-
- ManyLeft = getManyBuffersLeft();
-
- if (ManyLeft < MINBUFFERS)
- doit = true;
- else if (ManyLeft == _manyBuffers) /* All the buffers are already full */
- doit = false;
-
- if (doit && (ManyLeft < _manyBuffers) && ManyLeft) {
- ManyFill = _manyBuffers - ManyLeft - 1;
-
- while (ManyFill > 0) {
- _musicFilledTo++;
-
- if (_musicFilledTo >= _manyBuffers)
- _musicFilledTo = 0;
-
- fillbuffer(_musicFilledTo);
- updateMusic();
-
- ManyFill--;
- }
-
- updateMusic();
-
- ManyLeft = getManyBuffersLeft();
-
- if (ManyLeft < _manyBuffers) {
- ManyFill = _manyBuffers - ManyLeft - 1;
-
- while (ManyFill > 0) {
- _musicFilledTo++;
-
- if (_musicFilledTo >= _manyBuffers)
- _musicFilledTo = 0;
-
- fillbuffer(_musicFilledTo);
- updateMusic();
-
- ManyFill--;
- }
- }
- }
-
- updateMusic();
-
- /* NYI: A check for dirty cds; for instance, if lots of buffers already
- played */
}
-
-
/*****************************************************************************/
/* Starts up the music initially. */
/*****************************************************************************/
void Music::startMusic(bool startatbegin) {
- uint16 counter;
-
if (!_musicOn)
return;
@@ -217,32 +118,18 @@ void Music::startMusic(bool startatbegin) {
if (startatbegin) {
_file->seek(0);
- _leftinfile = _filelength;
+ _leftinfile = _file->size();
}
- _musicPlaying = 0;
- _musicOnBuffer = 0;
- _musicFilledTo = _manyBuffers - 1;
-
- _musicOn = false;
-
- for (counter = 0; counter < _manyBuffers; counter++)
- fillbuffer(counter);
-
_musicOn = true;
updateMusic();
}
-
-
-
-
/*****************************************************************************/
/* Initializes the music buffers. */
/*****************************************************************************/
bool Music::initMusic() {
- uint16 counter;
if (!_turnMusicOn)
return true;
@@ -257,12 +144,6 @@ bool Music::initMusic() {
else
filename = "Music:BackGrou";
- if (_musicBuffer[0] == NULL) {
- for (counter = 0; counter < _manyBuffers; counter++)
- _musicBuffer[counter] = malloc(MUSICBUFSIZE);
- }
-
- _filelength = sizeOfFile(filename);
_file = openPartial(filename);
if (_file) {
@@ -298,10 +179,7 @@ void Music::pauseBackMusic() {
_musicOn = false;
flushAudio();
- if (_musicPlaying)
- _musicPlaying--;
- else
- _musicPlaying = _manyBuffers - 1;
+ // TODO: Pause
_musicPaused = true;
}
@@ -373,13 +251,12 @@ void Music::changeMusic(const char *newmusic) {
if (!_tFile) {
_tFile = _file;
_tMusicOn = _musicOn;
- _tFileLength = _filelength;
#if defined(DOSCODE)
_tLeftInFile = _leftinfile;
#else
_tLeftInFile = _leftinfile + 65536L;
- if (_tLeftInFile > _tFileLength)
+ if (_tLeftInFile > (uint32)_tFile->size())
_tLeftInFile = _leftinfile;
#endif
@@ -390,9 +267,6 @@ void Music::changeMusic(const char *newmusic) {
if (_file) {
_musicOn = true; /* turn it off */
setMusic(false);
-
- _filelength = sizeOfFile(newmusic);
-
_musicOn = false; /* turn it back on */
setMusic(true);
} else {
@@ -414,10 +288,9 @@ void Music::resetMusic() {
_file->close();
_file = _tFile;
- _filelength = _tFileLength;
_leftinfile = _tLeftInFile;
- _file->seek(_filelength - _leftinfile);
+ _file->seek(_file->size() - _leftinfile);
_musicOn = true;
setMusic(false);
@@ -435,14 +308,6 @@ void Music::resetMusic() {
}
-
-
-
-#define FUDGEFACTOR 5L
-#define READSPEED (2 * 130000L)
-
-
-
/*****************************************************************************/
/* Checks whether or note enough memory in music buffer before loading any */
/* files. Fills it if not. Does not take into account the current buffer */
@@ -452,8 +317,6 @@ void Music::resetMusic() {
/* Here, the seconds are multipled by 10. */
/*****************************************************************************/
byte **Music::newOpen(const char *name) {
- uint32 filelength, LeftSecs, Time;
-
byte **file;
if (name == NULL) {
@@ -470,19 +333,7 @@ byte **Music::newOpen(const char *name) {
if (_musicOn) {
updateMusic();
-
- if (g_lab->getPlatform() == Common::kPlatformWindows)
- LeftSecs = (getManyBuffersLeft() * MUSICBUFSIZE * 10) / (2 * SAMPLESPEED); // Windows (16-bit)
- else
- LeftSecs = (getManyBuffersLeft() * MUSICBUFSIZE * 10) / SAMPLESPEED; // DOS (8-bit)
-
- filelength = sizeOfFile(name) * 10;
- Time = 10 + /* Seek time for the music and the file */
- (filelength / READSPEED) + /* Read time for the file */
- FUDGEFACTOR;
-
- if (Time >= LeftSecs)
- fillUpMusic(true);
+ fillUpMusic(true);
}
if (!_doNotFileFlushAudio && EffectPlaying)
@@ -493,36 +344,4 @@ byte **Music::newOpen(const char *name) {
return file;
}
-
-
-/*****************************************************************************/
-/* Checks whether or note enough memory in music buffer to continue loading */
-/* in a file. Fills the music buffer if not. Does not take into account */
-/* the current buffer playing; a built in fudge factor. We've also got */
-/* another FUDGEFACTOR defined above in case things go wrong. */
-/* */
-/* Here, the seconds are multipled by 10. */
-/*****************************************************************************/
-void Music::fileCheckMusic(uint32 filelength) {
- uint32 LeftSecs, Time;
-
- if (_musicOn) {
- updateMusic();
-
-
- if (g_lab->getPlatform() == Common::kPlatformWindows)
- LeftSecs = (getManyBuffersLeft() * MUSICBUFSIZE * 10) / (2 * SAMPLESPEED); // Windows (16-bit)
- else
- LeftSecs = (getManyBuffersLeft() * MUSICBUFSIZE * 10) / SAMPLESPEED; // DOS (8-bit)
-
- filelength *= 10;
- Time = 5 + /* Seek time for the music */
- (filelength / READSPEED) + /* Read time for the file */
- FUDGEFACTOR;
-
- if (Time >= LeftSecs)
- fillUpMusic(true);
- }
-}
-
} // End of namespace Lab