mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-05-02 14:57:59 +03:00
Added support for libsndfile (sound input)
This commit is contained in:
parent
a69938364f
commit
ddfbcecfcd
6 changed files with 168 additions and 1 deletions
50
sound/sources/libsndfile.cpp
Normal file
50
sound/sources/libsndfile.cpp
Normal file
|
@ -0,0 +1,50 @@
|
|||
#include "libsndfile.hpp"
|
||||
|
||||
#include "../../tools/str_exception.hpp"
|
||||
#include <sndfile.h>
|
||||
|
||||
using namespace Mangle::Stream;
|
||||
|
||||
static void fail(const std::string &msg)
|
||||
{ throw str_exception("Mangle::libsndfile: " + msg); }
|
||||
|
||||
using namespace Mangle::Sound;
|
||||
|
||||
void SndFileSource::getInfo(int32_t *_rate, int32_t *_channels, int32_t *_bits)
|
||||
{
|
||||
*_rate = rate;
|
||||
*_channels = channels;
|
||||
*_bits = bits;
|
||||
}
|
||||
|
||||
size_t SndFileSource::readSamples(void *data, size_t length)
|
||||
{
|
||||
// Read frames. We count channels as part of the frame. Even though
|
||||
// libsndfile does not, since it still requires the number of frames
|
||||
// read to be a multiple of channels.
|
||||
return channels*sf_read_short((SNDFILE*)handle, (short*)data, length*channels);
|
||||
}
|
||||
|
||||
SndFileSource::SndFileSource(const std::string &file)
|
||||
{
|
||||
SF_INFO info;
|
||||
info.format = 0;
|
||||
handle = sf_open(file.c_str(), SFM_READ, &info);
|
||||
if(handle == NULL)
|
||||
fail("Failed to open " + file);
|
||||
|
||||
// I THINK that using sf_read_short forces the library to convert to
|
||||
// 16 bits no matter what, but the libsndfile docs aren't exactly
|
||||
// very clear on this point.
|
||||
channels = info.channels;
|
||||
rate = info.samplerate;
|
||||
bits = 16;
|
||||
|
||||
// 16 bits per sample times number of channels
|
||||
setup(2*channels);
|
||||
}
|
||||
|
||||
SndFileSource::~SndFileSource()
|
||||
{
|
||||
sf_close((SNDFILE*)handle);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue