dungeon_crawler

view prototype/src/audio/source.cc @ 48:aa9e28670ae2

added sound playback, more to do
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 17 Sep 2012 08:40:59 +0300
parents
children d57df51f6b50
line source
1 #include "openal.h"
2 #include "source.h"
4 AudioSource::AudioSource()
5 {
6 sample = 0;
8 alGenSources(1, &alsrc);
9 alSourcei(alsrc, AL_LOOPING, AL_TRUE);
10 }
12 AudioSource::~AudioSource()
13 {
14 if(alsrc) {
15 if(is_playing()) {
16 stop();
17 }
18 alDeleteSources(1, &alsrc);
19 }
20 }
22 void AudioSource::set_sample(const AudioSample *sample)
23 {
24 stop();
26 if(sample) {
27 if(!sample->albuffer) {
28 fprintf(stderr, "%s: trying to attach null buffer!\n", __FUNCTION__);
29 return;
30 }
31 alSourcei(alsrc, AL_BUFFER, sample->albuffer);
32 }
33 this->sample = sample;
34 }
36 const AudioSample *AudioSource::get_sample() const
37 {
38 return sample;
39 }
41 void AudioSource::set_position(const Vector3 &pos, bool viewspace)
42 {
43 alSourcei(alsrc, AL_SOURCE_RELATIVE, viewspace ? AL_TRUE : AL_FALSE);
44 alSource3f(alsrc, AL_POSITION, pos.x, pos.y, pos.z);
45 }
47 Vector3 AudioSource::get_position() const
48 {
49 float pos[3];
50 alGetSourcefv(alsrc, AL_POSITION, pos);
51 return Vector3(pos[0], pos[1], pos[2]);
52 }
54 bool AudioSource::is_playing() const
55 {
56 int state;
57 alGetSourcei(alsrc, AL_SOURCE_STATE, &state);
58 return state == AL_PLAYING;
59 }
61 void AudioSource::play()
62 {
63 if(sample) {
64 alSourcePlay(alsrc);
65 }
66 }
68 void AudioSource::stop()
69 {
70 if(sample) {
71 alSourceStop(alsrc);
72 }
73 }