void playStream (int a, bool isMOD, bool loopy) { if (! soundOK) return; ALboolean ok; ALuint src; soundThing *st; void (*eos_callback)(void *userdata, ALuint source); if (isMOD) { st = &modCache[a]; eos_callback = mod_eos_callback; } else { st = &soundCache[a]; eos_callback = sound_eos_callback; } alGenSources(1, &src); if(alGetError() != AL_NO_ERROR) { debugOut( "Failed to create OpenAL source!\n"); return; } if (isMOD) { alSourcef (src, AL_GAIN, (float) modLoudness * defVol / 256); } else { alSourcef (src, AL_GAIN, (float) soundCache[a].vol / 256); } if (loopy) { ok = alurePlaySourceStream(src, (*st).stream, NUM_BUFS, -1, eos_callback, &intpointers[a]); } else { ok = alurePlaySourceStream(src, (*st).stream, NUM_BUFS, 0, eos_callback, &intpointers[a]); } if(!ok) { debugOut("Failed to play stream: %s\n", alureGetErrorString()); alDeleteSources(1, &src); if(alGetError() != AL_NO_ERROR) { debugOut("Failed to delete OpenAL source!\n"); } (*st).playingOnSource = 0; } else { (*st).playingOnSource = src; (*st).playing = true; } }
void SoundPlayer::play(std::string filename) { if(__SoundPlayer_audio_is_disabled) { return; } pthread_mutex_lock(&control_mutex); _player_stop(); pthread_mutex_lock(&player_mutex); stop = true; stream = alureCreateStreamFromFile(filename.c_str(), 19200, 2, buf); if(stream == AL_NONE) { printf("Error loading file %s: %s\n", filename.c_str(), alureGetErrorString()); }else if(!ALSources::get(sourceID)) { printf("No OpenAL sources available\n"); alureDestroyStream(stream, 1, buf); } else { stop = paused = false; alSourcef(sourceID, AL_GAIN, volume); alSource3f(sourceID, AL_POSITION, x, y, z); alurePlaySourceStream(sourceID, stream, 2, 0, stopped_callback, this); } pthread_mutex_unlock(&control_mutex); }
void playMovieStream (int a) { if (! soundOK) return; ALboolean ok; ALuint src; alGenSources(1, &src); if(alGetError() != AL_NO_ERROR) { debugOut( "Failed to create OpenAL source!\n"); return; } alSourcef (src, AL_GAIN, (float) soundCache[a].vol / 256); ok = alurePlaySourceStream(src, soundCache[a].stream, 10, 0, sound_eos_callback, &intpointers[a]); if(!ok) { debugOut("Failed to play stream: %s\n", alureGetErrorString()); alDeleteSources(1, &src); if(alGetError() != AL_NO_ERROR) { debugOut("Failed to delete OpenAL source!\n"); } soundCache[a].playingOnSource = 0; } else { soundCache[a].playingOnSource = src; soundCache[a].playing = true; } }
void PlayMusic(const std::string& name) { StopMusic(); size_t length; ALubyte* data = GetSoundData("Music/" + name, length); currentMusicName = name; currentMusicStream = alureCreateStreamFromStaticMemory(data, length, CHUNK_LENGTH, 2, musicBufs); alurePlaySourceStream(musicSource, currentMusicStream, 2, 1, MusicEndCallback, data); LOG("Sound", LOG_MESSAGE, "Music: %s", name.c_str()); if (!data) LOG("Sound", LOG_WARNING, "failed to find music"); }
void playSoundList(soundList *s) { if (soundOK) { cacheLoopySound = true; int a = cacheSound (s->sound); if (a == -1) { debugOut("Failed to cache sound!\n"); return; } soundCache[a].looping = false; if (s->vol < 0) soundCache[a].vol = defSoundVol; else soundCache[a].vol = s->vol; s-> cacheIndex = a; ALboolean ok; ALuint src; soundThing *st; st = &soundCache[a]; alGenSources(1, &src); if(alGetError() != AL_NO_ERROR) { debugOut("Failed to create OpenAL source!\n"); return; } alSourcef (src, AL_GAIN, (float) soundCache[a].vol / 256); ok = alurePlaySourceStream(src, (*st).stream, NUM_BUFS, 0, list_eos_callback, s); if(!ok) { debugOut("Failed to play stream: %s\n", alureGetErrorString()); alDeleteSources(1, &src); if(alGetError() != AL_NO_ERROR) { debugOut("Failed to delete OpenAL source!\n"); } (*st).playingOnSource = 0; } else { (*st).playingOnSource = src; (*st).playing = true; } } }
void PlayMusic(const std::string& name) { StopMusic(); size_t length; ALubyte* data = GetSoundData("Music/" + name, length); currentMusicName = name; currentMusicStream = alureCreateStreamFromStaticMemory(data, length, CHUNK_LENGTH, 2, musicBufs); alurePlaySourceStream(musicSource, currentMusicStream, 2, 1, MusicEndCallback, data); LOG("Sound", LOG_MESSAGE, "Music: %s", name.c_str()); if (!data) LOG("Sound", LOG_WARNING, "failed to find music"); #ifndef NDEBUG if (alGetError()) LOG("Sound", LOG_ERROR, "OpenAL error"); const ALchar* err = alureGetErrorString(); if (strcmp(err, "No error")) { LOG("Sound", LOG_ERROR, "ALURE error attempting to play song %s: %s", name.c_str(), err); exit(1); } #endif }
int main () { if (!alureInitDevice(NULL, NULL)) { std::fprintf(stderr, "Failed to open OpenAL device: %s\n", alureGetErrorString()); return 1; } alGenSources(1, &src); if (alGetError() != AL_NO_ERROR) { std::fprintf(stderr, "Failed to create OpenAL source!\n"); alureShutdownDevice(); return 1; } // Seeting Blip Buffer synth.treble_eq( -18.0f ); synth.volume (0.80); synth.output (&blipbuf); // Init Blip Buffer with a buffer of 250ms (second paramter is time in ms) if ( blipbuf.set_sample_rate( SR, 1000 / 4 ) ) { std::fprintf(stderr, "Failed to create Blip Buffer! Our of Memory\n"); alureShutdownDevice(); return 1; } blipbuf.clock_rate( blipbuf.sample_rate() ); blipbuf.bass_freq(300); // Equalization like a TV speaker stream = alureCreateStreamFromCallback (StreamCB, nullptr, AL_FORMAT_MONO16, SR, SR/2, 0, nullptr); if(!stream) { std::fprintf(stderr, "Error creating stream! %s\n", alureGetErrorString()); alDeleteSources(1, &src); alureShutdownDevice(); return 1; } if (!alurePlaySourceStream(src, stream, 4, 0, eos_callback, NULL)) { std::fprintf(stderr, "Failed to play stream: %s\n", alureGetErrorString()); isdone = 1; } alureUpdateInterval(0.005f); // Should be a independint thread playing the stream while(!isdone) { freq -= 1; if (freq < 1) { freq = 600; } alureSleep(0.02f); } alureStopSource(src, AL_FALSE); alDeleteSources(1, &src); alureDestroyStream(stream, 0, NULL); alureShutdownDevice(); return 0; }