Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
10 KB
Referenced Files
None
Subscribers
None
diff --git a/util/music-player.cpp b/util/music-player.cpp
index 65fd6e84..8f3ea297 100644
--- a/util/music-player.cpp
+++ b/util/music-player.cpp
@@ -1,126 +1,273 @@
#include "music-player.h"
#include "globals.h"
#include <iostream>
#include "configuration.h"
+#include "funcs.h"
+#include "system.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(){
}
-#ifdef USE_ALLEGRO
-
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 "";
}
}
-DumbPlayer::DumbPlayer(const char * path):
-volume(1.0){
+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 : {
- music_file = dumb_load_xm_quick(path);
+ what = dumb_load_xm_quick(path);
break;
}
case 1 : {
- music_file = dumb_load_s3m_quick(path);
+ what = dumb_load_s3m_quick(path);
break;
}
case 2 : {
- music_file = dumb_load_it_quick(path);
+ what = dumb_load_it_quick(path);
break;
}
case 3 : {
- music_file = dumb_load_mod_quick(path);
+ what = dumb_load_mod_quick(path);
break;
}
}
-
- if ( music_file != NULL ){
- Global::debug(0) << "Loaded " << path << " type " << typeToExtension( i ) << "(" << i << ")" << std::endl;
- break;
+
+ if (what != NULL){
+ Global::debug(0) << "Loaded " << path << " type " << typeToExtension(i) << "(" << i << ")" << std::endl;
+ return what;
}
}
+ return NULL;
+}
+
+#ifdef USE_ALLEGRO
- if (music_file){
+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, 22050);
+ 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
+
+ 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){
}
DumbPlayer::~DumbPlayer(){
+ Mix_HookMusic(NULL, NULL);
}
#endif
}
diff --git a/util/music-player.h b/util/music-player.h
index 6440a391..66f171c8 100644
--- a/util/music-player.h
+++ b/util/music-player.h
@@ -1,43 +1,63 @@
#ifndef _paintown_music_player_h
#define _paintown_music_player_h
+#ifdef USE_SDL
+/* for Uint8 */
+#include <SDL.h>
+#endif
+
struct DUH;
+struct DUH_SIGRENDERER;
#ifdef USE_ALLEGRO
struct AL_DUH_PLAYER;
#endif
namespace Util{
class MusicPlayer{
public:
MusicPlayer();
virtual void play() = 0;
virtual void poll() = 0;
virtual void pause() = 0;
virtual void setVolume(double volume) = 0;
virtual ~MusicPlayer();
};
/* interface to DUMB, plays mod/s3m/xm/it */
class DumbPlayer: public MusicPlayer {
public:
DumbPlayer(const char * path);
virtual void play();
virtual void poll();
virtual void pause();
virtual void setVolume(double volume);
virtual ~DumbPlayer();
+ static const int FREQUENCY = 22050;
+
+protected:
+ DUH * loadDumbFile(const char * path);
+
+#ifdef USE_SDL
+ static void mixer(void * player, Uint8 * stream, int length);
+ void render(Uint8 * stream, int length);
+#endif
+
protected:
#ifdef USE_ALLEGRO
AL_DUH_PLAYER * player;
#endif
DUH * music_file;
+#ifdef USE_SDL
+ DUH_SIGRENDERER * renderer;
+#endif
+
double volume;
};
}
#endif
diff --git a/util/sdl/sound.cpp b/util/sdl/sound.cpp
index 2dd8824e..b4bbf200 100644
--- a/util/sdl/sound.cpp
+++ b/util/sdl/sound.cpp
@@ -1,71 +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 = 44100;
- // Uint16 audio_format = AUDIO_S16;
- Uint16 audio_format = MIX_DEFAULT_FORMAT;
+ int audio_rate = 22050;
+ // 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);
}
}

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
69088
Default Alt Text
(10 KB)

Event Timeline