Context::Context(Device &dev)
{
- context=alcCreateContext(dev.get_device(), 0);
+ context = alcCreateContext(dev.get_device(), 0);
if(!context)
throw Exception("Couldn't create OpenAL context");
Device::Device()
{
- dev=alcOpenDevice(0);
+ dev = alcOpenDevice(0);
if(!dev)
throw Exception("Couldn't get OpenAL device");
}
Device::Device(const string &spec)
{
- dev=alcOpenDevice(spec.c_str());
+ dev = alcOpenDevice(spec.c_str());
if(!dev)
throw Exception("Couldn't get OpenAL device");
}
void Jukebox::add_track(const string &trk)
{
- bool was_empty=tracks.empty();
+ bool was_empty = tracks.empty();
tracks.push_back(trk);
if(was_empty)
{
- current_track=tracks.begin();
+ current_track = tracks.begin();
signal_track_changed.emit(*current_track);
}
}
void Jukebox::remove_track(const string &trk)
{
- list<string>::iterator i=find(tracks.begin(), tracks.end(), trk);
+ list<string>::iterator i = find(tracks.begin(), tracks.end(), trk);
if(i!=tracks.end())
{
if(i==current_track)
next();
tracks.erase(i);
if(tracks.empty())
- current_track=tracks.end();
+ current_track = tracks.end();
}
}
{
stop();
tracks.clear();
- current_track=tracks.end();
+ current_track = tracks.end();
}
const string &Jukebox::get_current_track() const
void Jukebox::set_shuffle(bool s)
{
- shuffle=s;
+ shuffle = s;
}
void Jukebox::play()
if(tracks.empty() || sound)
return;
- sound=new AL::Sound;
+ sound = new AL::Sound;
sound->open_file(*current_track);
streamer.play(*sound);
}
{
while(1)
{
- list<string>::iterator i=tracks.begin();
+ list<string>::iterator i = tracks.begin();
advance(i, rand()%tracks.size());
if(i!=current_track)
{
{
++current_track;
if(current_track==tracks.end())
- current_track=tracks.begin();
+ current_track = tracks.begin();
}
signal_track_changed.emit(*current_track);
}
if(tracks.size()>1)
{
if(current_track==tracks.begin())
- current_track=tracks.end();
+ current_track = tracks.end();
--current_track;
signal_track_changed.emit(*current_track);
}
{
streamer.stop();
delete sound;
- sound=0;
+ sound = 0;
}
void Jukebox::tick()
size_t memory_read(void *ptr, size_t size, size_t nmemb, void *src)
{
- MemorySource &memsrc=*reinterpret_cast<MemorySource *>(src);
- unsigned len=min(size*nmemb, memsrc.length-memsrc.pos);
+ MemorySource &memsrc = *reinterpret_cast<MemorySource *>(src);
+ unsigned len = min(size*nmemb, memsrc.length-memsrc.pos);
memcpy(ptr, reinterpret_cast<const char *>(memsrc.data)+memsrc.pos, len);
- memsrc.pos+=len;
+ memsrc.pos += len;
return len/size;
}
int memory_seek(void *src, ogg_int64_t offset, int whence)
{
- MemorySource &memsrc=*reinterpret_cast<MemorySource *>(src);
+ MemorySource &memsrc = *reinterpret_cast<MemorySource *>(src);
if(whence==SEEK_SET)
- memsrc.pos=offset;
+ memsrc.pos = offset;
else if(whence==SEEK_CUR)
- memsrc.pos+=offset;
+ memsrc.pos += offset;
else if(whence==SEEK_END)
- memsrc.pos=memsrc.length-offset;
- memsrc.pos=min(memsrc.pos, memsrc.length);
+ memsrc.pos = memsrc.length-offset;
+ memsrc.pos = min(memsrc.pos, memsrc.length);
return memsrc.pos;
}
long memory_tell(void *src)
{
- MemorySource &memsrc=*reinterpret_cast<MemorySource *>(src);
+ MemorySource &memsrc = *reinterpret_cast<MemorySource *>(src);
return memsrc.pos;
}
data(0),
eof_flag(false)
{
- ovfile.datasource=0;
+ ovfile.datasource = 0;
}
Sound::~Sound()
if(ovfile.datasource)
throw InvalidState("Sound has already been opened");
- MemorySource *src=new MemorySource(d, len);
+ MemorySource *src = new MemorySource(d, len);
if(ov_open_callbacks(src, &ovfile, 0, 0, memory_callbacks)<0)
{
delete src;
if(data)
throw InvalidState("Data has already been loaded");
- size=ov_pcm_total(&ovfile, 0)*4;
- char *dptr=new char[size];
- unsigned pos=0;
- while(unsigned len=read(dptr+pos, size-pos))
- pos+=len;
- data=dptr;
- size=pos;
- read_pos=0;
+ size = ov_pcm_total(&ovfile, 0)*4;
+ char *dptr = new char[size];
+ unsigned pos = 0;
+ while(unsigned len = read(dptr+pos, size-pos))
+ pos += len;
+ data = dptr;
+ size = pos;
+ read_pos = 0;
}
void Sound::load_file(const std::string &fn)
void Sound::rewind()
{
if(data)
- read_pos=0;
+ read_pos = 0;
else
ov_pcm_seek(&ovfile, 0);
}
{
if(data)
{
- len=min(len, size-read_pos);
+ len = min(len, size-read_pos);
memcpy(buf, data+read_pos, len);
- read_pos+=len;
+ read_pos += len;
return len;
}
else if(ovfile.datasource)
{
- int section=0;
- int res=ov_read(&ovfile, buf, len, 0, 2, 1, §ion);
+ int section = 0;
+ int res = ov_read(&ovfile, buf, len, 0, 2, 1, §ion);
if(res<0)
throw Exception("Error reading ogg vorbis file");
else if(res==0)
- eof_flag=true;
+ eof_flag = true;
return res;
}
else
void Sound::open_common()
{
delete data;
- data=0;
+ data = 0;
- vorbis_info *info=ov_info(&ovfile, -1);
- freq=info->rate;
+ vorbis_info *info = ov_info(&ovfile, -1);
+ freq = info->rate;
switch(info->channels)
{
- case 1: format=MONO16; break;
- case 2: format=STEREO16; break;
+ case 1: format = MONO16; break;
+ case 2: format = STEREO16; break;
default: throw Exception("Unsupported number of channels");
}
}
Source &SoundScape::play(const Buffer &buf, float x, float y, float z)
{
- Source *src=new Source;
+ Source *src = new Source;
add_source(*src);
src->set_buffer(buf);
src->set_position(x, y, z);
if((*i)->get_state()==STOPPED)
{
delete *i;
- i=sources.erase(i);
+ i = sources.erase(i);
}
else
++i;
void Source::queue_buffer(const Buffer &buffer)
{
- uint bid=buffer.get_id();
+ uint bid = buffer.get_id();
alSourceQueueBuffers(id, 1, &bid);
}
void Source::unqueue_buffer(const Buffer &buffer)
{
- uint bid=buffer.get_id();
+ uint bid = buffer.get_id();
alSourceUnqueueBuffers(id, 1, &bid);
}
unsigned Source::get_buffers_queued() const
{
- int n=0;
+ int n = 0;
get_attribute(AL_BUFFERS_QUEUED, &n);
return n;
}
unsigned Source::get_buffers_processed() const
{
- int n=0;
+ int n = 0;
get_attribute(AL_BUFFERS_PROCESSED, &n);
return n;
}
void Streamer::play(Sound &s)
{
- snd=&s;
+ snd = &s;
tick();
src.play();
}
void Streamer::stop()
{
- snd=0;
+ snd = 0;
src.stop();
}
void Streamer::tick()
{
- if(unsigned n=src.get_buffers_processed())
+ if(unsigned n = src.get_buffers_processed())
{
for(unsigned i=0; i<n; ++i)
{
if(src.get_state()!=PLAYING && src.get_state()!=PAUSED)
src.play();
- unsigned freq=snd->get_frequency();
- unsigned chunk_size=freq&~0xF;
- unsigned queued=src.get_buffers_queued();
+ unsigned freq = snd->get_frequency();
+ unsigned chunk_size = freq&~0xF;
+ unsigned queued = src.get_buffers_queued();
for(unsigned i=queued; i<4; ++i)
{
char data[chunk_size];
- unsigned pos=0;
+ unsigned pos = 0;
while(pos<chunk_size)
{
- unsigned len=snd->read(data+pos, chunk_size-pos);
+ unsigned len = snd->read(data+pos, chunk_size-pos);
if(len==0)
break;
- pos+=len;
+ pos += len;
}
if(pos)
{
- Buffer *buf=new Buffer;
+ Buffer *buf = new Buffer;
buf->data(snd->get_format(), data, pos, freq);
src.queue_buffer(*buf);
buffers.push_back(buf);
}
if(snd->eof())
- snd=0;
+ snd = 0;
}
} // namespace AL