dungeon_crawler

view prototype/src/audio/source.cc @ 80:a373b36ffc17

better
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 27 Oct 2012 01:59:39 +0300
parents aa9e28670ae2
children
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 void AudioSource::set_volume(float vol)
55 {
56 alSourcef(alsrc, AL_GAIN, vol);
57 }
59 float AudioSource::get_volume() const
60 {
61 float vol;
62 alGetSourcef(alsrc, AL_GAIN, &vol);
63 return vol;
64 }
66 void AudioSource::set_reference_dist(float rdist)
67 {
68 alSourcef(alsrc, AL_REFERENCE_DISTANCE, rdist);
69 }
71 float AudioSource::get_reference_dist() const
72 {
73 float res;
74 alGetSourcef(alsrc, AL_REFERENCE_DISTANCE, &res);
75 return res;
76 }
78 bool AudioSource::is_playing() const
79 {
80 int state;
81 alGetSourcei(alsrc, AL_SOURCE_STATE, &state);
82 return state == AL_PLAYING;
83 }
85 void AudioSource::play()
86 {
87 if(sample) {
88 alSourcePlay(alsrc);
89 }
90 }
92 void AudioSource::stop()
93 {
94 if(sample) {
95 alSourceStop(alsrc);
96 }
97 }