Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
7 KB
Referenced Files
None
Subscribers
None
diff --git a/util/allegro/sound.cpp b/util/allegro/sound.cpp
index 974c0b75..0d33b1d0 100644
--- a/util/allegro/sound.cpp
+++ b/util/allegro/sound.cpp
@@ -1,107 +1,104 @@
#include <allegro.h>
#include <string>
#include "../memory.h"
#include "../sound.h"
#include "configuration.h"
#include "../load_exception.h"
using namespace std;
Sound::Sound():
-my_sound(NULL),
own(NULL){
}
Sound::Sound(const char * data, int length):
-my_sound(NULL),
own(NULL){
PACKFILE_VTABLE table = Memory::makeTable();
Memory::memory memory((unsigned char *) data, length);
PACKFILE * pack = pack_fopen_vtable(&table, &memory);
- my_sound = load_wav_pf(pack);
+ this->data.sample = load_wav_pf(pack);
pack_fclose(pack);
- if (!my_sound){
+ if (!this->data.sample){
throw LoadException("Could not load wav data");
}
own = new int;
*own = 1;
}
Sound::Sound(const string & path) throw( LoadException ):
-my_sound(NULL ),
own(NULL){
- my_sound = load_sample( path.c_str() );
+ data.sample = load_sample( path.c_str() );
- if ( !my_sound ){
+ if ( !data.sample ){
string xf( "Could not load " );
xf += path;
throw LoadException(xf);
}
own = new int;
*own = 1;
}
void Sound::destroy(){
if ( own ){
*own -= 1;
if ( *own == 0 ){
delete own;
- destroy_sample( my_sound );
+ destroy_sample(data.sample);
own = NULL;
}
}
}
void Sound::initialize(){
/* default for alsa is 8, so reserve a few more */
reserve_voices(16, -1);
/* is calling this function a good idea? */
set_volume_per_voice(0);
// out<<"Install sound: "<<install_sound( DIGI_AUTODETECT, MIDI_NONE, "" )<<endl;
install_sound(DIGI_AUTODETECT, MIDI_NONE, "");
}
void Sound::stop(){
- if (my_sound){
- stop_sample(my_sound);
+ if (data.sample){
+ stop_sample(data.sample);
}
}
int scaleVolume(int v){
return (int)(v * (int) Configuration::getSoundVolume() / 100.0);
}
void Sound::play(){
- if (my_sound){
- play_sample(my_sound, scaleVolume(255), 128, 1000, false);
+ if (data.sample){
+ play_sample(data.sample, scaleVolume(255), 128, 1000, false);
}
}
void Sound::play(int volume, int pan){
- if ( my_sound ){
+ if ( data.sample){
int p = pan;
if ( p > 255 ){
p = 255;
} else if ( p < 0 ){
p = 0;
}
int v = volume;
if (v < 0){
v = 0;
} else if (v > 255){
v = 255;
}
- play_sample( my_sound, scaleVolume(v), p, 1000, false );
+ play_sample( data.sample, scaleVolume(v), p, 1000, false );
}
}
void Sound::playLoop(){
- if ( my_sound ){
- play_sample( my_sound, 255, 128, 1000, true );
+ if ( data.sample ){
+ play_sample( data.sample, 255, 128, 1000, true );
}
}
diff --git a/util/allegro/sound.h b/util/allegro/sound.h
new file mode 100644
index 00000000..a97989b9
--- /dev/null
+++ b/util/allegro/sound.h
@@ -0,0 +1,17 @@
+struct SAMPLE;
+
+struct SoundData{
+ SoundData():
+ sample(NULL){
+ }
+
+ SoundData(const SoundData & copy):
+ sample(copy.sample){}
+
+ SoundData & operator=(const SoundData & copy){
+ sample = copy.sample;
+ return *this;
+ }
+
+ SAMPLE * sample;
+};
diff --git a/util/sdl/sound.cpp b/util/sdl/sound.cpp
index a0d05e2e..2c167d92 100644
--- a/util/sdl/sound.cpp
+++ b/util/sdl/sound.cpp
@@ -1,63 +1,63 @@
#include "../sound.h"
#include <SDL.h>
#include "mixer/SDL_mixer.h"
Sound::Sound():
-my_sound(NULL),
own(NULL){
}
/* create from wav file (riff header + pcm) */
Sound::Sound(const char * data, int length){
/* TODO */
}
/* load from path */
Sound::Sound(const std::string & path) throw (LoadException){
- my_sound = (SAMPLE*) Mix_LoadWAV(path.c_str());
- if (!my_sound){
+ data.chunk = Mix_LoadWAV(path.c_str());
+ if (!data.chunk){
printf("Can't load sound %s\n", path.c_str());
// throw LoadException("Could not load sound " + path);
+ } else {
+ own = new int;
+ *own = 1;
}
- own = new int;
- *own = 1;
}
void Sound::initialize(){
int audio_rate = 22050;
// Uint16 audio_format = AUDIO_S16;
Uint16 audio_format = MIX_DEFAULT_FORMAT;
int audio_channels = 2;
int audio_buffers = 4096;
if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
printf("Unable to open audio: %s!\n", Mix_GetError());
// exit(1);
}
}
void Sound::play(){
- Mix_PlayChannel(-1, (Mix_Chunk*) my_sound, 0);
+ Mix_PlayChannel(-1, data.chunk, 0);
}
void Sound::play( int volume, int pan ){
- Mix_PlayChannel(-1, (Mix_Chunk*) my_sound, 0);
+ Mix_PlayChannel(-1, data.chunk, 0);
}
void Sound::playLoop(){
/* TODO */
}
void Sound::destroy(){
if ( own ){
*own -= 1;
if ( *own == 0 ){
delete own;
- Mix_FreeChunk((Mix_Chunk*) my_sound);
+ Mix_FreeChunk(data.chunk);
own = NULL;
}
}
}
void Sound::stop(){
/* TODO */
}
diff --git a/util/sdl/sound.h b/util/sdl/sound.h
new file mode 100644
index 00000000..f6d8b1f5
--- /dev/null
+++ b/util/sdl/sound.h
@@ -0,0 +1,17 @@
+struct Mix_Chunk;
+
+struct SoundData{
+ SoundData():
+ chunk(NULL){
+ }
+
+ SoundData(const SoundData & copy):
+ chunk(copy.chunk){}
+
+ SoundData & operator=(const SoundData & copy){
+ chunk = copy.chunk;
+ return *this;
+ }
+
+ Mix_Chunk * chunk;
+};
diff --git a/util/sound.cpp b/util/sound.cpp
index a19577a5..7b9631ea 100644
--- a/util/sound.cpp
+++ b/util/sound.cpp
@@ -1,34 +1,33 @@
#ifdef USE_ALLEGRO
#include "allegro/sound.cpp"
#endif
#ifdef USE_SDL
#include "sdl/sound.cpp"
#endif
Sound::Sound( const Sound & copy ):
-my_sound( NULL ),
own( NULL ){
own = copy.own;
if ( own ){
*own += 1;
}
- my_sound = copy.my_sound;
+ data = copy.data;
}
Sound & Sound::operator=( const Sound & rhs ){
if ( own ){
destroy();
}
own = rhs.own;
if ( own ){
*own += 1;
}
- my_sound = rhs.my_sound;
+
+ data = rhs.data;
return *this;
}
-
Sound::~Sound(){
destroy();
}
diff --git a/util/sound.h b/util/sound.h
index 990be278..9bf11bd7 100644
--- a/util/sound.h
+++ b/util/sound.h
@@ -1,41 +1,49 @@
#ifndef _paintown_sound_h
#define _paintown_sound_h
#include <string>
#include "load_exception.h"
+#ifdef USE_SDL
+#include "sdl/sound.h"
+#endif
+#ifdef USE_ALLEGRO
+#include "allegro/sound.h"
+#endif
+
struct SAMPLE;
/* a sound! */
class Sound{
public:
Sound();
/* create from wav file (riff header + pcm) */
Sound(const char * data, int length);
/* load from path */
Sound(const std::string & path) throw (LoadException);
Sound(const Sound & copy);
/* do any global initialization necessary */
static void initialize();
Sound & operator=( const Sound & rhs );
void play();
void play( int volume, int pan );
void playLoop();
void stop();
virtual ~Sound();
protected:
void destroy();
- SAMPLE * my_sound;
+ // SAMPLE * my_sound;
+ SoundData data;
/* reference counting */
int * own;
};
#endif

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 12:49 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69109
Default Alt Text
(7 KB)

Event Timeline