Skip to content
This repository has been archived by the owner on Apr 14, 2022. It is now read-only.

Commit

Permalink
add MixerImpl based on SDL2_mixer
Browse files Browse the repository at this point in the history
  • Loading branch information
cyxx committed Jun 24, 2015
1 parent ed95426 commit d91f76e
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 68 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
# BERMUDA_VORBIS : enable playback of digital soundtracks (22 khz mono .ogg files) # BERMUDA_VORBIS : enable playback of digital soundtracks (22 khz mono .ogg files)


#DEFINES = -DBERMUDA_WIN32 -DBERMUDA_VORBIS #DEFINES = -DBERMUDA_WIN32 -DBERMUDA_VORBIS
DEFINES = -DBERMUDA_POSIX -DBERMUDA_VORBIS #DEFINES = -DBERMUDA_POSIX -DBERMUDA_VORBIS
VORBIS_LIBS = -lvorbisfile -lvorbis -logg #VORBIS_LIBS = -lvorbisfile -lvorbis -logg
DEFINES = -DBERMUDA_POSIX


SDL_CFLAGS = `sdl2-config --cflags` SDL_CFLAGS = `sdl2-config --cflags`
SDL_LIBS = `sdl2-config --libs` SDL_LIBS = `sdl2-config --libs` -lSDL2_mixer


CXX = g++ CXX = g++
CXXFLAGS = -g -O -Wall $(SDL_CFLAGS) $(DEFINES) CXXFLAGS = -g -O -Wall $(SDL_CFLAGS) $(DEFINES)
Expand Down
2 changes: 1 addition & 1 deletion avi_player.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool AVI_Demuxer::readHeader() {
bool readHdrLoop = true; bool readHdrLoop = true;
while (readHdrLoop) { while (readHdrLoop) {
_f->read(tag, 4); _f->read(tag, 4);
int len = _f->readUint32_tLE(); int len = _f->readUint32LE();
assert((len & 1) == 0); assert((len & 1) == 0);
if (memcmp(tag, "LIST", 4) == 0) { if (memcmp(tag, "LIST", 4) == 0) {
_f->read(tag, 4); _f->read(tag, 4);
Expand Down
6 changes: 3 additions & 3 deletions dialogue.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -250,14 +250,14 @@ void Game::loadDialogueSprite(int spr) {
break; break;
} }
FileHolder fp(_fs, spriteFile); FileHolder fp(_fs, spriteFile);
int tag = fp->readUint16_tLE(); int tag = fp->readUint16LE();
if (tag != 0x3553) { if (tag != 0x3553) {
error("Invalid spr format %X", tag); error("Invalid spr format %X", tag);
} }
int count = fp->readUint16_tLE(); int count = fp->readUint16LE();
assert(count <= 105); assert(count <= 105);
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
int size = fp->readUint16_tLE(); int size = fp->readUint16LE();
_dialogueSpriteDataTable[spr][i] = (uint8_t *)malloc(size + 10); _dialogueSpriteDataTable[spr][i] = (uint8_t *)malloc(size + 10);
if (_dialogueSpriteDataTable[spr][i]) { if (_dialogueSpriteDataTable[spr][i]) {
fp->read(_dialogueSpriteDataTable[spr][i], size + 10); fp->read(_dialogueSpriteDataTable[spr][i], size + 10);
Expand Down
20 changes: 12 additions & 8 deletions file.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -93,21 +93,25 @@ struct File_stdio : File_impl {
File_impl *FileImpl_create() { return new File_stdio; } File_impl *FileImpl_create() { return new File_stdio; }
File_impl *FileImpl_create(uint32_t offset, uint32_t size) { return new File_stdio(offset, size); } File_impl *FileImpl_create(uint32_t offset, uint32_t size) { return new File_stdio(offset, size); }


File::File() { File::File()
: _path(0) {
_impl = FileImpl_create(); _impl = FileImpl_create();
} }


File::File(File_impl *impl) File::File(File_impl *impl)
: _impl(impl) { : _path(0), _impl(impl) {
} }


File::~File() { File::~File() {
free(_path);
_impl->close(); _impl->close();
delete _impl; delete _impl;
} }


bool File::open(const char *path, const char *mode) { bool File::open(const char *path, const char *mode) {
_impl->close(); _impl->close();
free(_path);
_path = strdup(path);
return _impl->open(path, mode); return _impl->open(path, mode);
} }


Expand Down Expand Up @@ -141,13 +145,13 @@ uint8_t File::readByte() {
return b; return b;
} }


uint16_t File::readUint16_tLE() { uint16_t File::readUint16LE() {
uint8_t b[2]; uint8_t b[2];
read(b, sizeof(b)); read(b, sizeof(b));
return READ_LE_UINT16(b); return READ_LE_UINT16(b);
} }


uint32_t File::readUint32_tLE() { uint32_t File::readUint32LE() {
uint8_t b[4]; uint8_t b[4];
read(b, sizeof(b)); read(b, sizeof(b));
return READ_LE_UINT32(b); return READ_LE_UINT32(b);
Expand All @@ -161,14 +165,14 @@ void File::writeByte(uint8_t b) {
write(&b, 1); write(&b, 1);
} }


void File::writeUint16_tLE(uint16_t n) { void File::writeUint16LE(uint16_t n) {
writeByte(n & 0xFF); writeByte(n & 0xFF);
writeByte(n >> 8); writeByte(n >> 8);
} }


void File::writeUint32_tLE(uint32_t n) { void File::writeUint32LE(uint32_t n) {
writeUint16_tLE(n & 0xFFFF); writeUint16LE(n & 0xFFFF);
writeUint16_tLE(n >> 16); writeUint16LE(n >> 16);
} }


struct MemoryMappedFile_impl { struct MemoryMappedFile_impl {
Expand Down
9 changes: 5 additions & 4 deletions file.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ struct File {
void seek(int offs, int origin = SEEK_SET); void seek(int offs, int origin = SEEK_SET);
uint32_t read(void *ptr, uint32_t len); uint32_t read(void *ptr, uint32_t len);
uint8_t readByte(); uint8_t readByte();
uint16_t readUint16_tLE(); uint16_t readUint16LE();
uint32_t readUint32_tLE(); uint32_t readUint32LE();
void write(void *ptr, uint32_t size); void write(void *ptr, uint32_t size);
void writeByte(uint8_t b); void writeByte(uint8_t b);
void writeUint16_tLE(uint16_t n); void writeUint16LE(uint16_t n);
void writeUint32_tLE(uint32_t n); void writeUint32LE(uint32_t n);


char *_path;
File_impl *_impl; File_impl *_impl;
}; };


Expand Down
140 changes: 132 additions & 8 deletions mixer.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright (C) 2007-2011 Gregory Montoir * Copyright (C) 2007-2011 Gregory Montoir
*/ */


#ifdef MIXER_SOFTWARE

#include "file.h" #include "file.h"
#include "mixer.h" #include "mixer.h"
#include "systemstub.h" #include "systemstub.h"
Expand Down Expand Up @@ -51,13 +53,13 @@ struct MixerChannel_Wav : MixerChannel {
f->seek(8); // skip RIFF header f->seek(8); // skip RIFF header
f->read(buf, 8); f->read(buf, 8);
if (memcmp(buf, "WAVEfmt ", 8) == 0) { if (memcmp(buf, "WAVEfmt ", 8) == 0) {
f->readUint32_tLE(); // fmtLength f->readUint32LE(); // fmtLength
int compression = f->readUint16_tLE(); int compression = f->readUint16LE();
int channels = f->readUint16_tLE(); int channels = f->readUint16LE();
int sampleRate = f->readUint32_tLE(); int sampleRate = f->readUint32LE();
f->readUint32_tLE(); // averageBytesPerSec f->readUint32LE(); // averageBytesPerSec
f->readUint16_tLE(); // blockAlign f->readUint16LE(); // blockAlign
_bitsPerSample = f->readUint16_tLE(); _bitsPerSample = f->readUint16LE();
if (compression != 1 || if (compression != 1 ||
(channels != 1 && channels != 2) || (channels != 1 && channels != 2) ||
(sampleRate != 11025 && sampleRate != 22050 && sampleRate != 44100) || (sampleRate != 11025 && sampleRate != 22050 && sampleRate != 44100) ||
Expand All @@ -69,7 +71,7 @@ struct MixerChannel_Wav : MixerChannel {
_bufReadStep = (sampleRate << _fracStepBits) / mixerSampleRate; _bufReadStep = (sampleRate << _fracStepBits) / mixerSampleRate;
f->read(buf, 4); f->read(buf, 4);
if (memcmp(buf, "data", 4) == 0) { if (memcmp(buf, "data", 4) == 0) {
_bufSize = f->readUint32_tLE(); _bufSize = f->readUint32LE();
_buf = (uint8_t *)malloc(_bufSize); _buf = (uint8_t *)malloc(_bufSize);
if (_buf) { if (_buf) {
f->read(_buf, _bufSize); f->read(_buf, _bufSize);
Expand Down Expand Up @@ -359,3 +361,125 @@ void Mixer::unbindChannel(int channel) {
_channels[channel] = 0; _channels[channel] = 0;
} }
} }
#else

#include <SDL.h>
#include <SDL_mixer.h>
#include "file.h"
#include "mixer.h"
#include "util.h"

struct MixerImpl {

static const int kMixFreq = 44100;
static const int kMixBufSize = 4096;
static const int kChannels = 4;

Mix_Chunk *_sounds[kChannels];
Mix_Music *_music;

virtual ~MixerImpl() {
}

virtual void open() {
memset(_sounds, 0, sizeof(_sounds));
_music = 0;

Mix_Init(MIX_INIT_OGG | MIX_INIT_FLUIDSYNTH);
if (Mix_OpenAudio(kMixFreq, AUDIO_S16SYS, 2, kMixBufSize) < 0) {
warning("Mix_OpenAudio failed: %s", Mix_GetError());
}
Mix_AllocateChannels(kChannels);
}
virtual void close() {
stopAll();
Mix_CloseAudio();
Mix_Quit();
}

virtual void playSoundWav(const char *path, int *id) {
debug(DBG_MIXER, "MixerImpl::playSoundWav()");
Mix_Chunk *chunk = Mix_LoadWAV(path);
if (chunk) {
*id = Mix_PlayChannel(-1, chunk, 0);
} else {
*id = -1;
}
}
virtual void playSoundMusic(const char *path, int *id) {
debug(DBG_MIXER, "MixerImpl::playSoundMusic()");
playMusic(path);
*id = -1;
}
virtual bool isPlaying(int id) {
return Mix_Playing(id) != 0;
}
virtual void stopSound(int id) {
debug(DBG_MIXER, "MixerImpl::stopSound()");
Mix_HaltChannel(id);
// Mix_FreeChunk
}

void playMusic(const char *path) {
stopMusic();
_music = Mix_LoadMUS(path);
if (_music) {
Mix_VolumeMusic(MIX_MAX_VOLUME / 2);
Mix_PlayMusic(_music, 0);
} else {
warning("Failed to load music '%s', %s", path, Mix_GetError());
}
}
void stopMusic() {
Mix_HaltMusic();
Mix_FreeMusic(_music);
_music = 0;
}

virtual void stopAll() {
debug(DBG_MIXER, "MixerImpl::stopAll()");
for (int i = 0; i < kChannels; ++i) {
stopSound(i);
}
stopMusic();
}
};

Mixer::Mixer(SystemStub *stub)
: _stub(stub) {
_impl = new MixerImpl;
}

Mixer::~Mixer() {
delete _impl;
}

void Mixer::open() {
_impl->open();
}

void Mixer::close() {
_impl->close();
}

void Mixer::playSoundWav(File *f, int *id) {
_impl->playSoundWav(f->_path, id);
}

void Mixer::playSoundVorbis(File *f, int *id) {
_impl->playSoundMusic(f->_path, id);
}

bool Mixer::isSoundPlaying(int id) {
return _impl->isPlaying(id);
}

void Mixer::stopSound(int id) {
_impl->stopSound(id);
}

void Mixer::stopAll() {
_impl->stopAll();
}

#endif
2 changes: 2 additions & 0 deletions mixer.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "intern.h" #include "intern.h"


struct File; struct File;
struct MixerImpl;
struct SystemStub; struct SystemStub;


struct MixerChannel { struct MixerChannel {
Expand Down Expand Up @@ -49,6 +50,7 @@ struct Mixer {
int _channelIdSeed; int _channelIdSeed;
bool _open; bool _open;
MixerChannel *_channels[kMaxChannels]; MixerChannel *_channels[kMaxChannels];
MixerImpl *_impl;
}; };


#endif // MIXER_H__ #endif // MIXER_H__
Loading

0 comments on commit d91f76e

Please sign in to comment.