Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
9 KB
Referenced Files
None
Subscribers
None
diff --git a/util/music-player.cpp b/util/music-player.cpp
index 8f3ea297..72b2025d 100644
--- a/util/music-player.cpp
+++ b/util/music-player.cpp
@@ -1,273 +1,180 @@
#include "music-player.h"
#include "globals.h"
#include <iostream>
#include "configuration.h"
-#include "funcs.h"
-#include "system.h"
+#include "sound.h"
#include "dumb/include/dumb.h"
#ifdef USE_ALLEGRO
#include "dumb/include/aldumb.h"
#ifdef _WIN32
/* what do we need winalleg for?
* reason: ...
*/
#include <winalleg.h>
#endif
#endif
#ifdef USE_SDL
#include "sdl/mixer/SDL_mixer.h"
#endif
namespace Util{
static double scaleVolume(double start){
return start * Configuration::getMusicVolume() / 100;
}
MusicPlayer::MusicPlayer(){
}
MusicPlayer::~MusicPlayer(){
}
static const char * typeToExtension( int i ){
switch (i){
case 0 : return ".xm";
case 1 : return ".s3m";
case 2 : return ".it";
case 3 : return ".mod";
default : return "";
}
}
DUH * DumbPlayer::loadDumbFile(const char * path){
DUH * what;
for (int i = 0; i < 4; i++){
/* the order of trying xm/s3m/it/mod matters because mod could be
* confused with one of the other formats, so load it last.
*/
switch (i){
case 0 : {
what = dumb_load_xm_quick(path);
break;
}
case 1 : {
what = dumb_load_s3m_quick(path);
break;
}
case 2 : {
what = dumb_load_it_quick(path);
break;
}
case 3 : {
what = dumb_load_mod_quick(path);
break;
}
}
if (what != NULL){
Global::debug(0) << "Loaded " << path << " type " << typeToExtension(i) << "(" << i << ")" << std::endl;
return what;
}
}
return NULL;
}
#ifdef USE_ALLEGRO
DumbPlayer::DumbPlayer(const char * path):
volume(1.0){
music_file = loadDumbFile(path);
if (music_file != NULL){
int buf = 1 << 11;
player = al_start_duh(music_file, 2, 0, scaleVolume(volume), buf, FREQUENCY);
}
}
void DumbPlayer::play(){
al_resume_duh(this->player);
}
void DumbPlayer::poll(){
if (al_poll_duh(this->player) != 0){
}
}
void DumbPlayer::pause(){
al_pause_duh(this->player);
}
void DumbPlayer::setVolume(double volume){
this->volume = volume;
al_duh_set_volume(player, scaleVolume(volume));
}
DumbPlayer::~DumbPlayer(){
al_stop_duh(player);
unload_duh(music_file);
}
#endif
-
#ifdef USE_SDL
DumbPlayer::DumbPlayer(const char * path):
volume(1.0){
music_file = loadDumbFile(path);
if (music_file == NULL){
/* FIXME */
throw std::exception();
}
int n_channels = 2;
int position = 0;
renderer = duh_start_sigrenderer(music_file, 0, n_channels, position);
if (!renderer){
Global::debug(0) << "Could not create renderer" << std::endl;
throw std::exception();
}
-
- // Mix_HookMusic(mixer, this);
-
- /*
- AL_DUH_PLAYER *dp;
-
- if (!duh)
- return NULL;
-
- dp = malloc(sizeof(*dp));
- if (!dp)
- return NULL;
-
- dp->flags = ADP_PLAYING;
- dp->bufsize = bufsize;
- dp->freq = freq;
-
- dp->stream = play_audio_stream(bufsize, 16, n_channels - 1, freq, 255, 128);
-
- if (!dp->stream) {
- free(dp);
- return NULL;
- }
-
- voice_set_priority(dp->stream->voice, 255);
-
- dp->sigrenderer = duh_start_sigrenderer(duh, 0, n_channels, pos);
-
- if (!dp->sigrenderer) {
- stop_audio_stream(dp->stream);
- free(dp);
- return NULL;
- }
-
- dp->volume = volume;
- dp->silentcount = 0;
-
- return dp;
- */
}
void DumbPlayer::render(Uint8 * stream, int length){
- int n;
- // Global::debug(0) << "Dumb render in " << (void*) stream << " buffer " << length << std::endl;
- double delta = 1.0;
- delta = 65536.0 / 22050;
- // delta = 1.0;
- // delta = 0.5;
- /*
- static int ok = 0;
- ok += 1;
- if (ok < 2){
- // memset(stream, 0, length);
- return;
- }
- ok = 0;
- */
- n = 0;
- n = duh_render(renderer, 16, 0, volume, delta, length / 4, stream);
- // Global::debug(0) << "Rendered " << n << " bits" << " at " << System::currentMicroseconds() << std::endl;
- // int n_channels = duh_sigrenderer_get_n_channels(dp->sigrenderer);
- // n *= n_channels;
- /*
- int size = length * n_channels;
- */
-#if 0
- Uint16 * s16 = (Uint16*) stream;
- int saw = 0;
- int f = 15000;
- for (; n < length; n += 2){
- /*
- if (n < length / 2){
- s16[n] = 32000;
- } else {
- s16[n] = -32000;
- }
- */
- s16[n] = f;
- saw += 1;
- if (saw > 200){
- f = -f;
- saw = 0;
- }
- // s16[n] = 10000 + n * 5;
- /*
- switch (n%4){
- case 0 : s16[n] = 0; break;
- case 1 : s16[n] = 16000; break;
- case 2 : s16[n] = 32000; break;
- case 3 : s16[n] = -32000; break;
- }
- */
- /*
- switch(rnd(2)){
- case 0 : s16[n] = 32000; break;
- case 1 : s16[n] = -32000;
- }
- */
- // s16[n] = rnd(65536) - 65536/2;
- // stream[n] = 0xa0;
- }
-#endif
+ double delta = 65536.0 / Sound::FREQUENCY;
+ /* a frame is 32 bits, 16 bit samples with 2 channels = 2 * 16,
+ * so we have to divide the number of 'dumb samples' by the
+ * size of each frame, 32 bits = 4 bytes
+ */
+ int n = duh_render(renderer, 16, 0, volume, delta, length / 4, stream);
+
if (n == 0){
// Global::debug(0) << "Sound finished?" << std::endl;
}
}
struct DumbPlayerInfo{
MusicPlayer * who;
};
void DumbPlayer::mixer(void * player_, Uint8 * stream, int length){
DumbPlayerInfo * info = (DumbPlayerInfo *) player_;
DumbPlayer * player = (DumbPlayer *) info->who;
player->render(stream, length);
}
void DumbPlayer::pause(){
}
void DumbPlayer::play(){
DumbPlayerInfo * me = new DumbPlayerInfo;
me->who = this;
Mix_HookMusic(mixer, me);
}
void DumbPlayer::poll(){
}
void DumbPlayer::setVolume(double volume){
+ this->volume = volume;
}
DumbPlayer::~DumbPlayer(){
Mix_HookMusic(NULL, NULL);
}
#endif
}
diff --git a/util/sdl/sound.cpp b/util/sdl/sound.cpp
index b4bbf200..10a2fe98 100644
--- a/util/sdl/sound.cpp
+++ b/util/sdl/sound.cpp
@@ -1,73 +1,73 @@
#include "../sound.h"
#include <SDL.h>
#include "mixer/SDL_mixer.h"
Sound::Sound():
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){
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 = 22050;
+ 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);
}
}
void Sound::play(){
/* FIXME: scale the volume based on the configuration sound setting */
Mix_VolumeChunk(data.chunk, MIX_MAX_VOLUME);
Mix_PlayChannel(-1, data.chunk, 0);
}
void Sound::play(double volume, int pan){
/* FIXME: scale the volume based on the configuration sound setting */
Mix_VolumeChunk(data.chunk, (int)(volume * MIX_MAX_VOLUME));
Mix_PlayChannel(-1, data.chunk, 0);
}
void Sound::playLoop(){
/* TODO */
}
void Sound::destroy(){
if ( own ){
*own -= 1;
if ( *own == 0 ){
delete own;
Mix_FreeChunk(data.chunk);
own = NULL;
}
}
}
void Sound::stop(){
if (data.channel != -1){
Mix_HaltChannel(data.channel);
}
}
diff --git a/util/sound.h b/util/sound.h
index 07b260e0..14ea08ba 100644
--- a/util/sound.h
+++ b/util/sound.h
@@ -1,49 +1,52 @@
#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(double volume, int pan);
void playLoop();
void stop();
virtual ~Sound();
+ /* global frequency to use */
+ static const int FREQUENCY = 22050;
+
protected:
void destroy();
// SAMPLE * my_sound;
SoundData data;
/* reference counting */
int * own;
};
#endif

File Metadata

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

Event Timeline