Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
9 KB
Referenced Files
None
Subscribers
None
diff --git a/util/allegro/sound.cpp b/util/allegro/sound.cpp
index 9d2e4073..8870c418 100644
--- a/util/allegro/sound.cpp
+++ b/util/allegro/sound.cpp
@@ -1,110 +1,105 @@
#include <allegro.h>
#include <string>
#include "../memory.h"
#include "../sound.h"
-#include "configuration.h"
#include "../load_exception.h"
using namespace std;
/* allegro uses volume in the range of 0-255 */
static const int MAX_VOLUME = 255;
Sound::Sound():
own(NULL){
}
Sound::Sound(const char * data, int length):
own(NULL){
PACKFILE_VTABLE table = Memory::makeTable();
Memory::memory memory((unsigned char *) data, length);
PACKFILE * pack = pack_fopen_vtable(&table, &memory);
this->data.sample = load_wav_pf(pack);
pack_fclose(pack);
if (!this->data.sample){
throw LoadException(__FILE__, __LINE__, "Could not load wav data");
}
own = new int;
*own = 1;
}
Sound::Sound(const string & path) throw( LoadException ):
own(NULL){
data.sample = load_sample( path.c_str() );
if ( !data.sample ){
string xf( "Could not load " );
xf += path;
throw LoadException(__FILE__, __LINE__, xf);
}
own = new int;
*own = 1;
}
void Sound::destroy(){
if ( own ){
*own -= 1;
if ( *own == 0 ){
delete own;
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::uninitialize(){
/* anything needed? */
}
void Sound::stop(){
if (data.sample){
stop_sample(data.sample);
}
}
-static double scaleVolume(double v){
- return v * Configuration::getSoundVolume() / 100.0;
-}
-
void Sound::play(){
if (data.sample){
- play_sample(data.sample, (int)(scaleVolume(1.0) * MAX_VOLUME), 128, 1000, false);
+ play_sample(data.sample, (int)(scale(1.0) * MAX_VOLUME), 128, 1000, false);
}
}
void Sound::play(double volume, int pan){
if ( data.sample){
if (pan > 255 ){
pan = 255;
} else if ( pan < 0 ){
pan = 0;
}
int v = volume;
if (v < 0){
v = 0;
} else if (v > 1){
v = 1;
}
- play_sample( data.sample, (int)(scaleVolume(v) * MAX_VOLUME), pan, 1000, false );
+ play_sample( data.sample, (int)(scale(v) * MAX_VOLUME), pan, 1000, false );
}
}
void Sound::playLoop(){
if ( data.sample ){
play_sample( data.sample, 255, 128, 1000, true );
}
}
diff --git a/util/allegro5/sound.cpp b/util/allegro5/sound.cpp
index 491427dd..230eb293 100644
--- a/util/allegro5/sound.cpp
+++ b/util/allegro5/sound.cpp
@@ -1,85 +1,85 @@
#include "../sound.h"
#include <allegro5/allegro5.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_memfile.h>
#include <allegro5/allegro_acodec.h>
Sound::Sound():
own(NULL){
}
/* create from wav file (riff header + pcm) */
Sound::Sound(const char * data, int length):
own(NULL){
ALLEGRO_FILE * memory = al_open_memfile((void*) data, length, "r");
this->data.sample = al_load_sample_f(memory, ".wav");
al_fclose(memory);
own = new int;
*own = 1;
}
/* load from path */
Sound::Sound(const std::string & path) throw (LoadException):
own(NULL){
data.sample = al_load_sample(path.c_str());
if (data.sample == NULL){
}
own = new int;
*own = 1;
/* TODO */
/*
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;
}
*/
}
void Sound::initialize(){
al_install_audio();
al_init_acodec_addon();
al_reserve_samples(8);
}
void Sound::uninitialize(){
}
void Sound::play(){
if (data.sample != NULL){
al_play_sample(data.sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
}
}
void Sound::play(double volume, int pan){
- /* FIXME: deal with volume */
+ /* FIXME: deal with pan */
if (data.sample != NULL){
- al_play_sample(data.sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
+ al_play_sample(data.sample, scale(volume), 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
}
}
void Sound::playLoop(){
if (data.sample != NULL){
- al_play_sample(data.sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
+ al_play_sample(data.sample, scale(1.0), 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
}
}
void Sound::stop(){
/* TODO */
}
void Sound::destroy(){
if (own){
*own -= 1;
if ( *own == 0 ){
delete own;
al_destroy_sample(data.sample);
own = NULL;
}
}
}
diff --git a/util/sdl/sound.cpp b/util/sdl/sound.cpp
index c42c1379..870c08c7 100644
--- a/util/sdl/sound.cpp
+++ b/util/sdl/sound.cpp
@@ -1,98 +1,93 @@
#include "../sound.h"
#include <SDL.h>
#include "mixer/SDL_mixer.h"
-#include "configuration.h"
Sound::Sound():
own(NULL){
}
/* create from wav file (riff header + pcm) */
Sound::Sound(const char * data, int length):
own(NULL){
SDL_RWops * ops = SDL_RWFromConstMem(data, length);
this->data.chunk = Mix_LoadWAV_RW(ops, 1);
own = new int;
*own = 1;
}
/* load from path */
Sound::Sound(const std::string & path) throw (LoadException):
own(NULL){
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;
}
}
void Sound::initialize(){
// int audio_rate = 22050;
/* allegro uses 44100 by default with alsa9 */
int audio_rate = FREQUENCY;
// int audio_rate = 22050;
Uint16 audio_format = AUDIO_S16;
// Uint16 audio_format = MIX_DEFAULT_FORMAT;
int audio_channels = 2;
int audio_buffers = 4096;
// int audio_buffers = 44100;
if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
printf("Unable to open audio: %s!\n", Mix_GetError());
// exit(1);
}
/* use the frequency enforced by the audio system */
Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
FREQUENCY = audio_rate;
}
void Sound::uninitialize(){
Mix_CloseAudio();
}
-static int scale(double in){
- return (int)(in * Configuration::getSoundVolume() / 100.0);
-}
-
void Sound::play(){
if (data.chunk != NULL){
- Mix_VolumeChunk(data.chunk, scale(MIX_MAX_VOLUME));
+ Mix_VolumeChunk(data.chunk, (int) scale(MIX_MAX_VOLUME));
Mix_PlayChannel(-1, data.chunk, 0);
}
}
void Sound::play(double volume, int pan){
if (data.chunk != NULL){
- Mix_VolumeChunk(data.chunk, scale(volume * MIX_MAX_VOLUME));
+ Mix_VolumeChunk(data.chunk, (int) scale(volume * MIX_MAX_VOLUME));
Mix_PlayChannel(-1, data.chunk, 0);
}
}
void Sound::playLoop(){
if (data.chunk != NULL){
- Mix_VolumeChunk(data.chunk, scale(MIX_MAX_VOLUME));
+ Mix_VolumeChunk(data.chunk, (int) scale(MIX_MAX_VOLUME));
Mix_PlayChannel(-1, data.chunk, -1);
}
}
void Sound::destroy(){
if (own){
*own -= 1;
if ( *own == 0 ){
delete own;
if (data.chunk != NULL){
Mix_FreeChunk(data.chunk);
}
own = NULL;
}
}
}
void Sound::stop(){
if (data.channel != -1){
Mix_HaltChannel(data.channel);
}
}
diff --git a/util/sound.cpp b/util/sound.cpp
index 711247f8..4f63e641 100644
--- a/util/sound.cpp
+++ b/util/sound.cpp
@@ -1,38 +1,44 @@
#ifdef USE_ALLEGRO
#include "allegro/sound.cpp"
#endif
#ifdef USE_SDL
#include "sdl/sound.cpp"
#endif
#ifdef USE_ALLEGRO5
#include "allegro5/sound.cpp"
#endif
+
+#include "configuration.h"
int Sound::FREQUENCY = 22050;
Sound::Sound( const Sound & copy ):
own( NULL ){
own = copy.own;
if ( own ){
*own += 1;
}
data = copy.data;
}
Sound & Sound::operator=( const Sound & rhs ){
if ( own ){
destroy();
}
own = rhs.own;
if ( own ){
*own += 1;
}
data = rhs.data;
return *this;
}
+double Sound::scale(double in){
+ return in * Configuration::getSoundVolume() / 100.0;
+}
+
Sound::~Sound(){
destroy();
}
diff --git a/util/sound.h b/util/sound.h
index bc8e851c..e884fe1f 100644
--- a/util/sound.h
+++ b/util/sound.h
@@ -1,58 +1,61 @@
#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
#ifdef USE_ALLEGRO5
#include "allegro5/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();
/* cleanup */
static void uninitialize();
Sound & operator=( const Sound & rhs );
void play();
void play(double volume, int pan);
void playLoop();
void stop();
virtual ~Sound();
/* global frequency to use */
// static const int FREQUENCY = 22050;
static int FREQUENCY;
protected:
+ /* scale to the configuration sound level */
+ static double scale(double in);
+
void destroy();
// SAMPLE * my_sound;
SoundData data;
/* reference counting */
int * own;
};
#endif

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 10:48 AM (3 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
68529
Default Alt Text
(9 KB)

Event Timeline