initial commit
This commit is contained in:
47
Sound.cpp
Normal file
47
Sound.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "Sound.h"
|
||||
|
||||
Sound::Sound() {
|
||||
is_open = false;
|
||||
stream = NULL;
|
||||
}
|
||||
|
||||
void Sound::setSound(SoundData *sound_data) {
|
||||
if (sound_data->len && !is_open) {
|
||||
//SDL_Log("audio open, len %d", sound_data->len);
|
||||
stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &sound_data->spec, &streamCb, sound_data);
|
||||
//stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &sound_data.spec, NULL, NULL);
|
||||
if (stream) {
|
||||
is_open = true;
|
||||
} else {
|
||||
SDL_Log("Could not open audio stream %s", SDL_GetError());
|
||||
}
|
||||
}
|
||||
if (stream && is_open) {
|
||||
if (sound_data->len) {
|
||||
SDL_SetAudioStreamGetCallback(stream, streamCb, sound_data);
|
||||
SDL_ResumeAudioStreamDevice(stream);
|
||||
} else {
|
||||
SDL_SetAudioStreamGetCallback(stream, NULL, sound_data);
|
||||
}
|
||||
/*int len = SDL_GetAudioStreamQueued(stream);
|
||||
SDL_Log("LEN %d paused %d", len);
|
||||
if (!len) {
|
||||
SDL_PutAudioStreamData(stream, sound_data.buf, sound_data.len);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
void Sound::streamCb(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount) {
|
||||
SoundData* sound_data = (SoundData *) userdata;
|
||||
if ((!sound_data->is_played || sound_data->loop) && additional_amount) {
|
||||
//SDL_Log("LEN2 %d total_amount %d additional_amount %d", sound_data->len, total_amount, additional_amount);
|
||||
SDL_PutAudioStreamData(stream, sound_data->buf, sound_data->len);
|
||||
sound_data->is_played = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Sound::closeSound() {
|
||||
SDL_DestroyAudioStream(stream);
|
||||
stream = NULL;
|
||||
is_open = false;
|
||||
}
|
||||
Reference in New Issue
Block a user