Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No sound outputs crash #411 #473

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 41 additions & 22 deletions lib/xsound/xsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//#include <SDL_mixer.h>
#include <clunk/clunk.h>

#include <memory>

#include "xglobal.h"
#include "xgraph.h"
#include "_xsound.h"
Expand Down Expand Up @@ -45,15 +47,14 @@ void xtDeactivateSysFinitFnc(int id);
static XSoundChannel channels[MAX_CHANNELS];

int XSoundError = 0;
int XSoundInitFlag = 0;
clunk::Context context;
clunk::Object * clunk_object;
std::unique_ptr<clunk::Context> context;
clunk::Object * clunk_object = nullptr;
int g_freq;


void SoundPlay(void *lpDSB, int channel, int priority, int cropos, int flags)
{
if(XSoundInitFlag){
if(clunk_object){
clunk::Sample *source = (clunk::Sample *)lpDSB;

//if (channel==2)
Expand Down Expand Up @@ -140,6 +141,10 @@ void SoundPlay(void *lpDSB, int channel, int priority, int cropos, int flags)

void SoundVolume(int channel, int volume)
{
if (!context)
{
return;
}
/* SDL_Mixer Version
volume+=10000;
volume*=0.0128;*/
Expand All @@ -148,21 +153,21 @@ void SoundVolume(int channel, int volume)
//Debug
//std::cout<<"SoundVolume:"<<channel<<" "<<volume<<std::endl;

if(XSoundInitFlag){
if(clunk_object){
if(channels[channel].sound) //channels[channel].sound -> SetVolume((LONG )volume);
{
channels[channel].volume = volume;
/* SDL_Mixer Version
Mix_Volume(channel, volume);*/
context.set_volume(channel, f_volume);
context->set_volume(channel, f_volume);
}
}
}

int GetSoundVolume(int channel)
{
int volume = 0;
if(XSoundInitFlag){
if(clunk_object){
std::cout<<"GetSoundVolume:"<<channel<<std::endl;
if(channels[channel].sound)
volume = channels[channel].volume;
Expand All @@ -180,7 +185,7 @@ void GlobalVolume(int volume)

void SoundPan(int channel, int panning)
{
if(XSoundInitFlag){
if(clunk_object){
//std::cout<<"SoundPan "<<channel<<" "<<panning*0.001<<std::endl;
channels[channel].pan = panning;
//if(channels[channel].sound) {
Expand All @@ -194,7 +199,7 @@ int GetSoundFrequency(void *lpDSB)
{
//std::cout<<"Get SoundFrequency"<<((clunk::Sample *)lpDSB)->name<<std::endl;
unsigned long ret = 0;
if(XSoundInitFlag){
if(clunk_object){
/*int numtimesopened, frequency, channels;
Uint16 format;
SDL_Mixer Version
Expand All @@ -209,7 +214,7 @@ int GetSoundFrequency(void *lpDSB)
void SetSoundFrequency(void *lpDSB,int frq)
{
//std::cout<<"SetSoundFrequency:"<<((clunk::Sample *)lpDSB)->name<<" frq:"<<frq<<std::endl;
if(XSoundInitFlag){
if(clunk_object){
float out_pitch = frq;
out_pitch/=22050;
//std::cout<<"pitch:"<<out_pitch<<" freq:"<<((clunk::Sample *)lpDSB)->spec.freq<<std::endl;
Expand All @@ -220,15 +225,15 @@ void SetSoundFrequency(void *lpDSB,int frq)
void* GetSound(int channel)
{
//std::cout<<"GetSound:"<<((clunk::Sample *)channels[channel].sound)->name<<" channel:"<<channel<<std::endl;
if(XSoundInitFlag)
if(clunk_object)
return channels[channel].sound;

return NULL;
}

void SoundStop(int channel)
{
if(XSoundInitFlag){
if(clunk_object){
//std::cout<<"SoundStop:"<<channel<<std::endl;
if(channels[channel].sound){
/* SDL_Mixer version
Expand All @@ -243,7 +248,7 @@ void SoundStop(int channel)

void SoundRelease(void *lpDSB)
{
if(XSoundInitFlag){
if(clunk_object){
//std::cout<<"SoundRelease:"<<((clunk::Sample *)lpDSB)->name<<std::endl;
/* SDL_Mixer version
Mix_FreeChunk((Mix_Chunk *)lpDSB);*/
Expand All @@ -254,6 +259,11 @@ void SoundRelease(void *lpDSB)

void SoundLoad(char *filename, void **lpDSB)
{
if (!context)
{
*lpDSB = nullptr;
return;
}
/* SDL_Mixer version
Mix_Chunk *chunk=Mix_LoadWAV(filename);
if(chunk==NULL)
Expand All @@ -267,7 +277,7 @@ void SoundLoad(char *filename, void **lpDSB)
//std::cout<<"SoundLoad:"<<filename<<std::endl;
clunk::Sample *chunk = NULL;
try {
chunk = context.create_sample();
chunk = context->create_sample();
chunk->load(filename);
} catch(const std::exception & e)
{
Expand Down Expand Up @@ -303,7 +313,7 @@ void SetVolume(void *lpDSB, int volume)
int GetVolume(void *lpDSB)
{
int ret = 0;
if(XSoundInitFlag){
if(clunk_object){
/* SDL_Mixer Version
ret = Mix_VolumeChunk((Mix_Chunk *)lpDSB, 0);
Mix_VolumeChunk((Mix_Chunk *)lpDSB, ret);*/
Expand All @@ -319,6 +329,7 @@ void ChannelFinished(int channel)

int SoundInit(int maxHZ, int schannels)
{
std::unique_ptr<clunk::Context> initializer(new clunk::Context());
//Only one freq, now sound more powerfull.
/* SDL_Mixer version
if (SDL_Init (SDL_INIT_AUDIO) == -1)
Expand All @@ -337,10 +348,19 @@ int SoundInit(int maxHZ, int schannels)

Mix_AllocateChannels(MAX_CHANNELS); */
//std::cout<<"SoundInit maxHZ:"<<maxHZ<<std::endl;
context.init(maxHZ, 2, 512);
try
{
initializer->init(maxHZ, 2, 512);
context = std::move(initializer);
}
catch (const std::exception &e)
{
std::cerr << "unable to initialize sound, reason: " << e.what() << std::endl;
return false;
}
g_freq = maxHZ;
context.set_max_sources(MAX_CHANNELS);
clunk_object = context.create_object();
context->set_max_sources(MAX_CHANNELS);
clunk_object = context->create_object();
int i;
for(i = 0; i < MAX_CHANNELS; ++i)
{
Expand All @@ -351,22 +371,21 @@ int SoundInit(int maxHZ, int schannels)
}
/* SDL_Mixer version
Mix_ChannelFinished(&ChannelFinished);*/
XSoundInitFlag = 1;
//ErrH.Abort("SoundInit OK!!!");
return true;
}

void SoundFinit(void)
{
int i;
if(XSoundInitFlag){
if(clunk_object){
for(i = 0; i < MAX_CHANNELS; ++i)
SoundStop(i);
/* SDL_Mixer version
Mix_CloseAudio();*/
delete clunk_object;
context.deinit();
XSoundInitFlag = 0;
context->deinit();
context.reset();
}
}

Expand Down
140 changes: 83 additions & 57 deletions src/sound/music.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//#include "PlayOgg.h"

#include <memory>
#include <sstream>

#include "../global.h"

#include "hsound.h"
#include "../../lib/xsound/ogg_stream.h"

extern clunk::Context context;
extern std::unique_ptr<clunk::Context> context;
//static MpegSound* music = 0;
//extern LPDIRECTSOUND lpDS;

Expand Down Expand Up @@ -59,41 +60,53 @@ void xsDeInitMusic(void) {
}

void xsPlayMusic(int track,int min = 0,int sec = 0) {
curTrack = track;
//music->OpenToPlay(getTrackPathName(track));
//music=Mix_LoadMUS(getTrackPathName(track));
//Mix_PlayMusic(music, 31);
//std::cout<<"xsPlayMusic:"<<curTrack<<std::endl;
context.play(-1, new OggStream(getTrackPathName(track)), true);
context.set_volume(-1, volume/256.0);
if (context)
{
curTrack = track;
//music->OpenToPlay(getTrackPathName(track));
//music=Mix_LoadMUS(getTrackPathName(track));
//Mix_PlayMusic(music, 31);
//std::cout<<"xsPlayMusic:"<<curTrack<<std::endl;
context->play(-1, new OggStream(getTrackPathName(track)), true);
context->set_volume(-1, volume / 256.0);
}
}

void xsPlayOneTrackMusic(int track,int min = 0,int sec = 0) {
curTrack = track;
//if (music)
// {
//Mix_HaltMusic();
//Mix_RewindMusic();
//Mix_FreeMusic(music);
//music = NULL;
//}
//music=Mix_LoadMUS(getTrackPathName(track));
//Mix_RewindMusic();
//Mix_PlayMusic(music, 31);
//music->OpenToPlay(getTrackPathName(track));
//std::cout<<"xsPlayOneTrackMusic:"<<curTrack<<std::endl;
if (context.playing(-1))
context.stop(-1);
context.play(-1, new OggStream(getTrackPathName(track)), true);
context.set_volume(-1, volume/256.0);
if (context)
{
//if (music)
// {
//Mix_HaltMusic();
//Mix_RewindMusic();
//Mix_FreeMusic(music);
//music = NULL;
//}
//music=Mix_LoadMUS(getTrackPathName(track));
//Mix_RewindMusic();
//Mix_PlayMusic(music, 31);
//music->OpenToPlay(getTrackPathName(track));
//std::cout<<"xsPlayOneTrackMusic:"<<curTrack<<std::endl;
if (context->playing(-1))
{
context->stop(-1);
}
context->play(-1, new OggStream(getTrackPathName(track)), true);
context->set_volume(-1, volume / 256.0);
}
}

void xsStopMusic(void) {
//music->Stop();
//Mix_HaltMusic();
if (context.playing(-1))
context.stop(-1);

if (context)
{
//music->Stop();
//Mix_HaltMusic();
if (context->playing(-1))
{
context->stop(-1);
}
}
}

void xsStopMusic(int &track) {
Expand All @@ -102,33 +115,43 @@ void xsStopMusic(int &track) {
}

void xsPauseMusic(void) {
//music->Pause();
//Mix_PausedMusic();
context.pause(-1);
if (context)
{
//music->Pause();
//Mix_PausedMusic();
context->pause(-1);
}
}

void xsResumeMusic(void) {
//music->Resume();
//Mix_ResumeMusic();
context.pause(-1);
if (context)
{
//music->Resume();
//Mix_ResumeMusic();
context->pause(-1);
}
}

int xsGetStatusMusic(void) {
/*switch (music->IsPlay()) {
case MPEG_STOP:
return XCD_STOPPED;
case MPEG_PAUSE:
return XCD_PAUSED;
case MPEG_PLAY:
return XCD_PLAYING;
default:
return XCD_OTHER;
}*/
if (context.playing(-1)) {
return XCD_PLAYING;
} else {
return XCD_STOPPED;
}
if (context)
{
/*switch (music->IsPlay()) {
case MPEG_STOP:
return XCD_STOPPED;
case MPEG_PAUSE:
return XCD_PAUSED;
case MPEG_PLAY:
return XCD_PLAYING;
default:
return XCD_OTHER;
}*/
if (context->playing(-1)) {
return XCD_PLAYING;
} else {
return XCD_STOPPED;
}
}

return XCD_OTHER;
}

Expand All @@ -155,13 +178,16 @@ int xsGetVolumeMusic(void) {
}

void xsSetVolumeMusic(int val) {
//std::cout<<"xsSetVolumeMusic:"<<val<<std::endl;
//music->SetVolume(val);
float f_val = val;
f_val/=256;
//Mix_VolumeMusic(val);
context.set_volume(-1, f_val);
volume=val;
if (context)
{
//std::cout<<"xsSetVolumeMusic:"<<val<<std::endl;
//music->SetVolume(val);
float f_val = val;
f_val /= 256;
//Mix_VolumeMusic(val);
context->set_volume(-1, f_val);
volume=val;
}
}