1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Erwin 'vaiursch' Herrsche |
---|
24 | * Co-authors: |
---|
25 | * |
---|
26 | */ |
---|
27 | #include "SoundStreamer.h" |
---|
28 | |
---|
29 | #include <boost/thread.hpp> |
---|
30 | #include <al.h> |
---|
31 | #include <alc.h> |
---|
32 | #include <vorbis/vorbisfile.h> |
---|
33 | #include "SoundManager.h" |
---|
34 | #include "util/Sleep.h" |
---|
35 | |
---|
36 | namespace orxonox |
---|
37 | { |
---|
38 | // vorbis callbacks |
---|
39 | size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource); |
---|
40 | int seekVorbis(void* datasource, ogg_int64_t offset, int whence); |
---|
41 | long tellVorbis(void* datasource); |
---|
42 | |
---|
43 | void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream) |
---|
44 | { |
---|
45 | COUT(4) << "Sound: Creating thread for " << dataStream->getName() << std::endl; |
---|
46 | // Open file with custom streaming |
---|
47 | ov_callbacks vorbisCallbacks; |
---|
48 | vorbisCallbacks.read_func = &readVorbis; |
---|
49 | vorbisCallbacks.seek_func = &seekVorbis; |
---|
50 | vorbisCallbacks.tell_func = &tellVorbis; |
---|
51 | vorbisCallbacks.close_func = NULL; |
---|
52 | |
---|
53 | OggVorbis_File vf; |
---|
54 | int ret = ov_open_callbacks(dataStream.get(), &vf, NULL, 0, vorbisCallbacks); |
---|
55 | if (ret < 0) |
---|
56 | { |
---|
57 | COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl; |
---|
58 | ov_clear(&vf); |
---|
59 | return; |
---|
60 | } |
---|
61 | vorbis_info* vorbisInfo; |
---|
62 | vorbisInfo = ov_info(&vf, -1); |
---|
63 | ALenum format; |
---|
64 | if (vorbisInfo->channels == 1) |
---|
65 | format = AL_FORMAT_MONO16; |
---|
66 | else |
---|
67 | format = AL_FORMAT_STEREO16; |
---|
68 | |
---|
69 | char inbuffer[4096]; |
---|
70 | ALuint initbuffers[5]; |
---|
71 | alGenBuffers(5, initbuffers); |
---|
72 | if (ALint error = alGetError()) { |
---|
73 | COUT(2) << "Sound: Streamer: Could not generate buffer:" << getALErrorString(error) << std::endl; |
---|
74 | return; |
---|
75 | } |
---|
76 | int current_section; |
---|
77 | |
---|
78 | for(int i = 0; i < 5; i++) |
---|
79 | { |
---|
80 | long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); |
---|
81 | if (ret == 0) |
---|
82 | { |
---|
83 | break; |
---|
84 | } |
---|
85 | else if (ret < 0) |
---|
86 | { |
---|
87 | COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; |
---|
88 | ov_clear(&vf); |
---|
89 | return; |
---|
90 | } |
---|
91 | |
---|
92 | alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate); |
---|
93 | if(ALint error = alGetError()) { |
---|
94 | COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl; |
---|
95 | break; |
---|
96 | } |
---|
97 | alSourceQueueBuffers(audioSource, 1, &initbuffers[i]); |
---|
98 | if (ALint error = alGetError()) { |
---|
99 | COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | while(true) // Stream forever, control through thread control |
---|
104 | { |
---|
105 | int processed; |
---|
106 | |
---|
107 | if(alcGetCurrentContext() == NULL) |
---|
108 | { |
---|
109 | COUT(2) << "Sound: There is no context, terminating thread" << std::endl; |
---|
110 | return; |
---|
111 | } |
---|
112 | |
---|
113 | alGetSourcei(audioSource, AL_BUFFERS_PROCESSED, &processed); |
---|
114 | if (ALint error = alGetError()) |
---|
115 | COUT(2) << "Sound: Warning: Couldn't get number of processed buffers: " << getALErrorString(error) << std::endl; |
---|
116 | |
---|
117 | if(processed > 0) |
---|
118 | { |
---|
119 | COUT(4) << "Sound: " << processed << std::endl; |
---|
120 | ALuint* buffers = new ALuint[processed]; |
---|
121 | alSourceUnqueueBuffers(audioSource, processed, buffers); |
---|
122 | if (ALint error = alGetError()) |
---|
123 | COUT(2) << "Sound: Warning: Couldn't unqueue buffers: " << getALErrorString(error) << std::endl; |
---|
124 | |
---|
125 | for(int i = 0; i < processed; i++) |
---|
126 | { |
---|
127 | long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); |
---|
128 | if (ret == 0) |
---|
129 | { |
---|
130 | return; |
---|
131 | } |
---|
132 | else if (ret < 0) |
---|
133 | { |
---|
134 | COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; |
---|
135 | ov_clear(&vf); |
---|
136 | return; |
---|
137 | } |
---|
138 | |
---|
139 | alBufferData(buffers[i], format, &inbuffer, ret, vorbisInfo->rate); |
---|
140 | if(ALint error = alGetError()) { |
---|
141 | COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl; |
---|
142 | break; |
---|
143 | } |
---|
144 | alSourceQueueBuffers(audioSource, 1, &buffers[i]); |
---|
145 | if (ALint error = alGetError()) { |
---|
146 | COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl; |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | try { |
---|
151 | boost::this_thread::interruption_point(); |
---|
152 | } |
---|
153 | catch(boost::thread_interrupted) { |
---|
154 | COUT(4) << "Sound: Catched interruption. Terminating thread for " << dataStream->getName() << std::endl; |
---|
155 | return; |
---|
156 | } |
---|
157 | msleep(100); // perhaps another value here is better |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|