Debian Codegraph @

src/game/audio/openalwavdata.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2013 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #define _CRT_SECURE_NO_WARNINGS
17
18 // Always enable asserts
19 #ifdef NDEBUG
20 #undef NDEBUG
21 #endif
22
23 #include "openalwavdata.hpp"
24
25 #include <AL/alc.h>
26 #include <cassert>
27 #include <cstdio>
28 #include <stdexcept>
29
30 static bool checkError()
31 {
32 ALCenum error = alGetError();
33 return error == AL_NO_ERROR;
34 }
35
36 // WAV load code from http://www.dunsanyinteractive.com/blogs/oliver/?p=72
37 // by Oliver Plunkett
38
39 struct RIFF_Header
40 {
41 char chunkID[4];
42 int32_t chunkSize;
43 char format[4];
44 };
45
46 struct WAVE_Format
47 {
48 char subChunkID[4];
49 int32_t subChunkSize;
50 int16_t audioFormat;
51 int16_t numChannels;
52 int32_t sampleRate;
53 int32_t byteRate;
54 int16_t blockAlign;
55 int16_t bitsPerSample;
56 };
57
58 struct WAVE_Data
59 {
60 char subChunkID[4]; // Should contain the word data
61 int32_t subChunk2Size; // Stores the size of the data block
62 };
63
64 static bool loadWavFile(
65 const std::string & filename,
66 ALuint * buffer,
67 ALsizei * size,
68 ALsizei * frequency,
69 ALenum * format)
70 {
71 FILE * soundFile = nullptr;
72 WAVE_Format wave_format;
73 RIFF_Header riff_header;
74 WAVE_Data wave_data;
75 unsigned char * data;
76
77 try
78 {
79 soundFile = fopen(filename.c_str(), "rb");
80 if (!soundFile)
81 throw std::runtime_error(filename);
82
83 // Read in the first chunk into the struct
84 size_t bytesRead = std::fread(&riff_header, sizeof(RIFF_Header), 1, soundFile);
85 assert(bytesRead == sizeof(RIFF_Header));
86
87 //check for RIFF and WAVE tag in memeory
88 if ((riff_header.chunkID[0] != 'R' || riff_header.chunkID[1] != 'I' || riff_header.chunkID[2] != 'F' || riff_header.chunkID[3] != 'F') || (riff_header.format[0] != 'W' || riff_header.format[1] != 'A' || riff_header.format[2] != 'V' || riff_header.format[3] != 'E'))
89 throw std::runtime_error("Invalid RIFF or WAVE Header");
90
91 //Read in the 2nd chunk for the wave info
92 bytesRead = std::fread(&wave_format, sizeof(WAVE_Format), 1, soundFile);
93 assert(bytesRead == sizeof(WAVE_Format));
94
95 //check for fmt tag in memory
96 if (wave_format.subChunkID[0] != 'f' || wave_format.subChunkID[1] != 'm' || wave_format.subChunkID[2] != 't' || wave_format.subChunkID[3] != ' ')
97 throw std::runtime_error("Invalid Wave Format");
98
99 //check for extra parameters;
100 if (wave_format.subChunkSize > 16)
101 std::fseek(soundFile, sizeof(int16_t), SEEK_CUR);
102
103 //Read in the the last byte of data before the sound file
104 bytesRead = std::fread(&wave_data, sizeof(WAVE_Data), 1, soundFile);
105 assert(bytesRead == sizeof(WAVE_Data));
106
107 //check for data tag in memory
108 if (wave_data.subChunkID[0] != 'd' || wave_data.subChunkID[1] != 'a' || wave_data.subChunkID[2] != 't' || wave_data.subChunkID[3] != 'a')
109 throw std::runtime_error("Invalid data header");
110
111 //Allocate memory for data
112 data = new unsigned char[wave_data.subChunk2Size];
113
114 // Read in the sound data into the soundData variable
115 if (!std::fread(data, static_cast<size_t>(wave_data.subChunk2Size), 1, soundFile))
116 throw std::runtime_error("error loading WAVE data into struct!");
117
118 //Now we set the variables that we passed in with the
119 //data from the structs
120 *size = wave_data.subChunk2Size;
121 *frequency = wave_format.sampleRate;
122 //The format is worked out by looking at the number of
123 //channels and the bits per sample.
124 if (wave_format.numChannels == 1)
125 {
126 if (wave_format.bitsPerSample == 8)
127 *format = AL_FORMAT_MONO8;
128 else if (wave_format.bitsPerSample == 16)
129 *format = AL_FORMAT_MONO16;
130 }
131 else if (wave_format.numChannels == 2)
132 {
133 if (wave_format.bitsPerSample == 8)
134 *format = AL_FORMAT_STEREO8;
135 else if (wave_format.bitsPerSample == 16)
136 *format = AL_FORMAT_STEREO16;
137 }
138 //create our openAL buffer and check for success
139 alGenBuffers(1, buffer);
140 checkError();
141 //now we put our data into the openAL buffer and
142 //check for success
143 alBufferData(*buffer, *format, reinterpret_cast<void *>(data), *size, *frequency);
144 checkError();
145 //clean up and return true if successful
146 fclose(soundFile);
147 return true;
148 } catch (std::string error)
149 {
150 //clean up memory if wave loading fails
151 std::fclose(soundFile);
152 //return false to indicate the failure to load wave
153 return false;
154 }
155 }
156
157 OpenALWavData::OpenALWavData(const std::string & path)
158 : m_size(0)
159 , m_freq(0)
160 , m_buffer(0)
161 {
162 OpenALWavData::load(path);
163 }
164
165 void OpenALWavData::load(const std::string & path)
166 {
167 OpenALData::load(path);
168
169 if (!loadWavFile(path, &m_buffer, &m_size, &m_freq, &m_format))
170 {
171 throw std::runtime_error("Failed to load '" + path + "'");
172 }
173
174 if (!checkError())
175 {
176 throw std::runtime_error("Failed to set buffer data of '" + path + "'");
177 }
178 }
179
180 ALuint OpenALWavData::buffer() const
181 {
182 return m_buffer;
183 }
184
185 OpenALWavData::~OpenALWavData()
186 {
187 alDeleteBuffers(1, &m_buffer);
188 }