void FakeAudio::loadAudio() { // this might take some time, we should move this to a bg thread buffer = manager.getBuffer(src); if (!buffer) { buffer = new OpenALBuffer(src); manager.setBuffer(buffer, src); } alGenSources(1, &sourceId); alSourcei(sourceId, AL_BUFFER, buffer->getBufferId()); alSourcei(sourceId, AL_PITCH, 1.0f); alSourcei(sourceId, AL_GAIN, 1.0f); loaded = true; if (autoplay) play(NULL, 0, NULL); if (loop) alSourcei(sourceId, AL_LOOPING, AL_TRUE); // get the duration ALint bufferID = buffer->getBufferId(), bufferSize, frequency, bitsPerSample, channels; alGetBufferi(bufferID, AL_SIZE, &bufferSize); alGetBufferi(bufferID, AL_FREQUENCY, &frequency); alGetBufferi(bufferID, AL_CHANNELS, &channels); alGetBufferi(bufferID, AL_BITS, &bitsPerSample); duration = ((float)bufferSize)/(frequency*channels*(bitsPerSample/8)); }
Buffer* LoadWAV(const char* name, const void* data, size_t size) { Buffer* b = new Buffer(name); b->bufferId[0] = alutCreateBufferFromFileImage(data,size); ALenum error = alutGetError(); if (error != ALUT_ERROR_NO_ERROR) { freeslw::ErrorPrintf(alutGetErrorString(error)); delete b; return 0; } if (b->bufferId[0] == NO_BUFFER) { freeslw::ErrorPrintf("Failed to create buffer for \"%s\"",name); delete b; return 0; } ALint format; ALint isize; ALint freq; ALint bits; ALint channels; ALint d = 1; alGetBufferi(b->bufferId[0],AL_CHANNELS,&format); alGetBufferi(b->bufferId[0],AL_SIZE,&isize); alGetBufferi(b->bufferId[0],AL_FREQUENCY,&freq); if (format == AL_FORMAT_MONO8) b->format = freeslw::TA_MONO8; else if (format == AL_FORMAT_MONO16) b->format = freeslw::TA_MONO16; else if (format == AL_FORMAT_STEREO8) b->format = freeslw::TA_STEREO8; else if (format == AL_FORMAT_STEREO16) b->format = freeslw::TA_STEREO16; else freeslw::ErrorPrintf("Failed to retrieve a valid sound format (format = %d)",format); alGetBufferi(b->bufferId[0],AL_BITS,&bits); alGetBufferi(b->bufferId[0],AL_CHANNELS,&channels); if (bits == 16 && channels == 1) d = 2; else if (bits == 8 && channels == 2) d = 2; else if (bits == 16 && channels == 2) d = 4; if (freq == 0) { freeslw::ErrorPrintf("\"%s\": Frequency = 0",name); freq = 1; } b->frequency = (int)freq; b->length = (isize * 1000) / (freq * d); b->activeBuffer = 0; b->numBuffers = 1; return b; }
//----------------------------------------------------------------------------// void SoundBuffer::updateInfo() { alGetBufferi(mBuffers[0], AL_FREQUENCY, &mFrequency); alGetBufferi(mBuffers[0], AL_BITS, &mBits); alGetBufferfv(mBuffers[0], AL_SIZE, &mSize); alGetBufferi(mBuffers[0], AL_CHANNELS, &mChannels); SoundSystem::checkError(__FUNCTION__); mDuration = 8.0f * float(mSize) / float(mBits * mFrequency * mChannels); }
/** * @brief Loads the sound. * * @param snd Sound to load. * @param filename Name of the file to load into sound. */ int sound_al_load( alSound *snd, const char *filename ) { int ret; SDL_RWops *rw; OggVorbis_File vf; ALint freq, bits, channels, size; /* get the file data buffer from packfile */ rw = ndata_rwops( filename ); /* Check to see if it's an OGG. */ if (ov_test_callbacks( rw, &vf, NULL, 0, sound_al_ovcall_noclose )==0) { ret = sound_al_loadOgg( snd, &vf ); } /* Otherwise try WAV. */ else { /* Destroy the partially loaded vorbisfile. */ ov_clear(&vf); /* Try to load Wav. */ ret = sound_al_loadWav( snd, rw ); } /* Close RWops. */ SDL_RWclose(rw); /* Failed to load. */ if (ret != 0) { WARN("Failed to load sound file '%s'.", filename); return ret; } soundLock(); /* Get the length of the sound. */ alGetBufferi( snd->u.al.buf, AL_FREQUENCY, &freq ); alGetBufferi( snd->u.al.buf, AL_BITS, &bits ); alGetBufferi( snd->u.al.buf, AL_CHANNELS, &channels ); alGetBufferi( snd->u.al.buf, AL_SIZE, &size ); if ((freq==0) || (bits==0) || (channels==0)) { WARN("Something went wrong when loading sound file '%s'.", filename); snd->length = 0; } else snd->length = (double)size / (double)(freq * (bits/8) * channels); /* Check for errors. */ al_checkErr(); soundUnlock(); return 0; }
void SoundBuffer::GetBufferProperties() { ASSERT( m_id != 0 ); alGetBufferi(m_id, AL_SIZE, &m_num_bytes); alGetBufferi(m_id, AL_CHANNELS, &m_channels); alGetBufferi(m_id, AL_BITS, &m_bits); alGetBufferi(m_id, AL_FREQUENCY, &m_freq); m_length_in_samples = m_num_bytes * 8 / (m_channels * m_bits); m_buffer_duration_seconds = (float)m_length_in_samples / (float)m_freq; }
float AudioPlayer::GetBufferLengthInSeconds() { ALint size, bits, freq; alGetBufferi(pBuffer->GetBufferID(), AL_SIZE, &size); alGetBufferi(pBuffer->GetBufferID(), AL_BITS, &bits); alGetBufferi(pBuffer->GetBufferID(), AL_FREQUENCY, &freq); if (alGetError() != AL_NO_ERROR) return -1.0f; else return (ALfloat)((ALuint)size / pBuffer->GetChannels() / (bits / 8)) / (ALfloat)freq; }
ALfloat GetBufferLength(ALuint buffer) { ALint size, bits, channels, freq; alGetBufferi(buffer, AL_SIZE, &size); alGetBufferi(buffer, AL_BITS, &bits); alGetBufferi(buffer, AL_CHANNELS, &channels); alGetBufferi(buffer, AL_FREQUENCY, &freq); if(alGetError() != AL_NO_ERROR) return -1.0f; return (ALfloat)((ALuint)size / channels / (bits / 8)) / (ALfloat)freq; }
float MALContext::getBufferDuration(unsigned int bufferId) { M_PROFILE_SCOPE(MALContext::getBufferDuration); int size; int bits; int freq; alGetBufferi(bufferId, AL_SIZE, &size); alGetBufferi(bufferId, AL_BITS, &bits); alGetBufferi(bufferId, AL_FREQUENCY, &freq); float duration = size / (float)((bits/8.0f) * freq); return duration; }
Sound::Sound(const char *file) : handle(alutCreateBufferFromFile(file)) { if (handle == AL_NONE) { throw ALError(alGetError(), "alutCreateBufferFromFile"); } ALint size, channels, bits, freq; alGetBufferi(handle, AL_SIZE, &size); alGetBufferi(handle, AL_CHANNELS, &channels); alGetBufferi(handle, AL_BITS, &bits); alGetBufferi(handle, AL_FREQUENCY, &freq); duration = size * 8 * 1000 / (channels * bits * freq); }
//////////////////////////////////////////////////////////// /// Get the sample rate //////////////////////////////////////////////////////////// unsigned int SoundBuffer::GetSampleRate() const { ALint SampleRate; ALCheck(alGetBufferi(myBuffer, AL_FREQUENCY, &SampleRate)); return SampleRate; }
//////////////////////////////////////////////////////////// /// Return the number of channels (1 = mono, 2 = stereo, ...) //////////////////////////////////////////////////////////// unsigned int SoundBuffer::GetChannelsCount() const { ALint ChannelsCount; ALCheck(alGetBufferi(myBuffer, AL_CHANNELS, &ChannelsCount)); return ChannelsCount; }
uint WSound::GetNumChannels(uint buffer) const { if (!m_bCheck() || buffer >= m_numBuffers) return 0; ALint out = 0; alGetBufferi(m_buffers[buffer], AL_CHANNELS, &out); return out; }
static void iterate( void ) { static ALfloat position[] = { 10.0, 0.0, 4.0 }; static ALfloat movefactor = 4.5; static time_t then = 0; time_t now; ALint byteloki; ALint size; now = time( NULL ); /* Switch between left and right stereo sample every two seconds. */ if( now - then > 2 ) { then = now; movefactor *= -1.0; } position[0] += movefactor; alSourcefv( moving_source, AL_POSITION, position ); micro_sleep(500000); alGetSourceiv( moving_source, AL_BYTE_LOKI, &byteloki); alGetBufferi( stereo, AL_SIZE, &size); fprintf(stderr, "byteloki = %d size = %d\n", byteloki, size); return; }
unsigned int SoundBuffer::getChannelCount() const { ALint channelCount; alCheck(alGetBufferi(m_buffer, AL_CHANNELS, &channelCount)); return channelCount; }
int lime_al_get_bufferi (int buffer, int param) { ALint data; alGetBufferi (buffer, param, &data); return data; }
unsigned int SoundBuffer::getSampleRate() const { ALint sampleRate; alCheck(alGetBufferi(m_buffer, AL_FREQUENCY, &sampleRate)); return sampleRate; }
uint WSound::GetBitDepth(uint buffer) const { if (!m_bCheck() || buffer >= m_numBuffers) return 0; ALint out = 0; alGetBufferi(m_buffers[buffer], AL_BITS, &out); return out; }
value lime_al_get_bufferi (value buffer, value param) { int data; alGetBufferi (val_int (buffer), val_int (param), &data); return alloc_int (data); }
float AudioPlayer::GetPosition() { ALint offset = 0; alGetSourcei(m_source[0], AL_BYTE_OFFSET, &offset); ALint tot = 0; alGetBufferi(pBuffer->GetBufferID(), AL_SIZE, &tot); return (GetBufferLengthInSeconds() * offset) / tot; }
float MALContext::getSourceTimePos(unsigned int sourceId) { M_PROFILE_SCOPE(MALContext::getSourceTimePos); int bits; int freq; int bOffset; ALint buffer; alGetSourcei(sourceId, AL_BUFFER, &buffer); alGetSourcei(sourceId, AL_BYTE_OFFSET, &bOffset); alGetBufferi(buffer, AL_BITS, &bits); alGetBufferi(buffer, AL_FREQUENCY, &freq); float timePos = bOffset / (float)((bits/8.0f) * freq); return timePos; }
void al_getbufferi( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { if (NULL == alGetBufferi) mogl_glunsupported("alGetBufferi"); alGetBufferi((ALuint)mxGetScalar(prhs[0]), (ALenum)mxGetScalar(prhs[1]), (ALint*)mxGetData(prhs[2])); }
void AudioPlayer::SeekOffset(float offset) { if (offset < 0 || offset > 1) return; ALint tot = 0; alGetBufferi(pBuffer->GetBufferID(), AL_SIZE, &tot); alSourcei(m_source[0], AL_BYTE_OFFSET, offset * tot); }
U32 LLAudioBufferOpenAL::getLength() { if(mALBuffer == AL_NONE) { return 0; } ALint length; alGetBufferi(mALBuffer, AL_SIZE, &length); return length / 2; // convert size in bytes to size in (16-bit) samples }
U32 LLAudioBufferOpenAL::getLength() { if(mALBuffer == AL_NONE) { return 0; } ALint length; alGetBufferi(mALBuffer, AL_SIZE, &length); return length >> 2; }
float Audio::Buffer :: length() const { if(Headless::enabled()) return 0.0f; assert(id > 0); ALint sz; ALint channels; ALint bits; ALint freq; alGetBufferi(id, AL_SIZE, &sz); alGetBufferi(id, AL_CHANNELS, &channels); alGetBufferi(id, AL_BITS, &bits); alGetBufferi(id, AL_FREQUENCY, &freq); unsigned samples = sz * 8 / (channels * bits); return (float)samples / (float)freq; }
void C4MusicFileOgg::Execute() { if (playing) { // get processed buffer count ALint num_processed = 0; alErrorCheck(alGetSourcei(channel, AL_BUFFERS_PROCESSED, &num_processed)); bool done = false; while (num_processed--) { // release processed buffer ALuint buffer; alErrorCheck(alSourceUnqueueBuffers(channel, 1, &buffer)); // add playback time of processed buffer to total playback time ALint buf_bits = 16, buf_chans = 2, buf_freq = 44100; alErrorCheck(alGetBufferi(buffer, AL_BITS, &buf_bits)); alErrorCheck(alGetBufferi(buffer, AL_CHANNELS, &buf_chans)); alErrorCheck(alGetBufferi(buffer, AL_FREQUENCY, &buf_freq)); double buffer_secs = double(buffer_size) / buf_bits / buf_chans / buf_freq * 8; last_playback_pos_sec += buffer_secs; // refill processed buffer size_t buffer_idx; for (buffer_idx=0; buffer_idx<num_buffers; ++buffer_idx) if (buffers[buffer_idx] == buffer) break; if (!done) done = !FillBuffer(buffer_idx); } if (done) streaming_done = true; // check if done ALint state = 0; alErrorCheck(alGetSourcei(channel, AL_SOURCE_STATE, &state)); if (state != AL_PLAYING && streaming_done) { Stop(); // reset playback to beginning for next time this piece is playing last_playback_pos_sec = 0.0; } else if (state == AL_STOPPED) { alErrorCheck(alSourcePlay(channel)); } } }
/** Play a 44100Hz mono 16bit PCM sound */ void Audio::playMono16Sound(const QByteArray& data) { QMutexLocker lock(&audioOutLock); if (!alOutDev) { if (!openOutput(Settings::getInstance().getOutDev())) return; } ALuint buffer; alGenBuffers(1, &buffer); alBufferData(buffer, AL_FORMAT_MONO16, data.data(), data.size(), 44100); alSourcef(alMainSource, AL_GAIN, outputVolume); alSourcei(alMainSource, AL_BUFFER, buffer); alSourcePlay(alMainSource); ALint sizeInBytes; ALint channels; ALint bits; alGetBufferi(buffer, AL_SIZE, &sizeInBytes); alGetBufferi(buffer, AL_CHANNELS, &channels); alGetBufferi(buffer, AL_BITS, &bits); int lengthInSamples = sizeInBytes * 8 / (channels * bits); ALint frequency; alGetBufferi(buffer, AL_FREQUENCY, &frequency); qreal duration = (lengthInSamples / static_cast<qreal>(frequency)) * 1000; int remaining = timer->interval(); if (duration > remaining) timer->start(duration); alDeleteBuffers(1, &buffer); }
AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum param, ALint *values) { ALCdevice *device; ALCcontext *context; ALbuffer *albuf; switch(param) { case AL_FREQUENCY: case AL_BITS: case AL_CHANNELS: case AL_SIZE: case AL_INTERNAL_FORMAT_SOFT: case AL_BYTE_LENGTH_SOFT: case AL_SAMPLE_LENGTH_SOFT: case AL_UNPACK_BLOCK_ALIGNMENT_SOFT: case AL_PACK_BLOCK_ALIGNMENT_SOFT: alGetBufferi(buffer, param, values); return; } context = GetContextRef(); if(!context) return; device = context->Device; LockBuffersRead(device); if((albuf=LookupBuffer(device, buffer)) == NULL) SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done); if(!(values)) SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done); switch(param) { case AL_LOOP_POINTS_SOFT: ReadLock(&albuf->lock); values[0] = albuf->LoopStart; values[1] = albuf->LoopEnd; ReadUnlock(&albuf->lock); break; default: SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done); } done: UnlockBuffersRead(device); ALCcontext_DecRef(context); }
void alGetBufferiv(ALuint buffer, ALenum pname, ALint *values) { DEBUGLOGCALL(LCF_OPENAL); if (values == nullptr) { return; } switch(pname) { case AL_FREQUENCY: case AL_BITS: case AL_CHANNELS: case AL_SIZE: alGetBufferi(buffer, pname, values); return; default: ALSETERROR(AL_INVALID_VALUE); return; } }
static int StreamPoll(V3XA_STREAM handle) { ALint processed; // ALint queued; ALint size; // ALint status; int ret_code = -1; if (!g_pstreams[handle].used) { return -1; } ChannelSetParms(g_pstreams[handle].chan, &g_ListenerPos, NULL, NULL); alGetSourcei(g_pchannels[g_pstreams[handle].chan].src, AL_BUFFERS_PROCESSED, &processed); while (processed > 0) { alSourceUnqueueBuffers(g_pchannels[g_pstreams[handle].chan].src, 1, &g_pstreams[handle].bufs[g_pstreams[handle].first]); alGetBufferi(g_pstreams[handle].bufs[g_pstreams[handle].first], AL_SIZE, &size); g_pstreams[handle].pos += size; alDeleteBuffers(1, &g_pstreams[handle].bufs[g_pstreams[handle].first]); g_pstreams[handle].first ++; if (g_pstreams[handle].first > g_nbufsperstream) { g_pstreams[handle].first = 0; } processed --; } if (((g_pstreams[handle].last >= g_pstreams[handle].first) && (g_pstreams[handle].last - g_pstreams[handle].first <= 20)) || ((g_pstreams[handle].last < g_pstreams[handle].first) && (g_pstreams[handle].last + g_nbufsperstream - g_pstreams[handle].first <= 20))) { ret_code = 0; } return ret_code; }