Otclient  14/8/2020
soundfile.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "soundfile.h"
24 #include "oggsoundfile.h"
26 
28 {
29  m_file = fileStream;
30 }
31 
32 SoundFilePtr SoundFile::loadSoundFile(const std::string& filename)
33 {
34  stdext::timer t;
35  FileStreamPtr file = g_resources.openFile(filename);
36  if(!file)
37  stdext::throw_exception(stdext::format("unable to open %s", filename));
38 
39  // cache file buffer to avoid lags from hard drive
40  file->cache();
41 
42  char magic[4];
43  file->read(magic, 4);
44  file->seek(0);
45 
46  SoundFilePtr soundFile;
47  if(strncmp(magic, "OggS", 4) == 0) {
48  OggSoundFilePtr oggSoundFile = OggSoundFilePtr(new OggSoundFile(file));
49  if(oggSoundFile->prepareOgg())
50  soundFile = oggSoundFile;
51  } else
52  stdext::throw_exception(stdext::format("unknown sound file format %s", filename));
53 
54  return soundFile;
55 }
56 
58 {
59  if(m_channels == 2) {
60  if(m_bps == 16)
61  return AL_FORMAT_STEREO16;
62  else if(m_bps == 8)
63  return AL_FORMAT_STEREO8;
64  } else if(m_channels == 1) {
65  if(m_bps == 16)
66  return AL_FORMAT_MONO16;
67  else if(m_bps == 8)
68  return AL_FORMAT_MONO8;
69  }
70  return AL_UNDETERMINED;
71 }
FileStream::seek
void seek(uint pos)
Definition: filestream.cpp:142
stdext::timer
Definition: time.h:36
SoundFile::m_channels
int m_channels
Definition: soundfile.h:50
SoundFile::m_bps
int m_bps
Definition: soundfile.h:52
SoundFile::m_file
FileStreamPtr m_file
Definition: soundfile.h:49
resourcemanager.h
OggSoundFile
Definition: oggsoundfile.h:30
stdext::format
std::string format()
Definition: format.h:82
OggSoundFilePtr
stdext::shared_object_ptr< OggSoundFile > OggSoundFilePtr
Definition: declarations.h:48
SoundFile::getSampleFormat
ALenum getSampleFormat()
Definition: soundfile.cpp:57
g_resources
ResourceManager g_resources
Definition: resourcemanager.cpp:32
soundfile.h
FileStream::cache
void cache()
Definition: filestream.cpp:58
oggsoundfile.h
FileStream::read
int read(void *buffer, uint size, uint nmemb=1)
Definition: filestream.cpp:109
stdext::throw_exception
void throw_exception(const std::string &what)
Throws a generic exception.
Definition: exception.h:43
ResourceManager::openFile
FileStreamPtr openFile(const std::string &fileName)
Definition: resourcemanager.cpp:232
SoundFile::SoundFile
SoundFile(const FileStreamPtr &fileStream)
Definition: soundfile.cpp:27
stdext::shared_object_ptr< FileStream >
SoundFile::loadSoundFile
static SoundFilePtr loadSoundFile(const std::string &filename)
Definition: soundfile.cpp:32