libsndfile

From vegard.wiki
Jump to navigation Jump to search
#include <memory>
#include <stdexcept>

extern "C" {
#include <sndfile.h>
}

struct sound_sample {
        SF_INFO info;
        std::unique_ptr<float []> buffer;

        sound_sample(const char *filename):
                buffer(nullptr)
        {
                std::unique_ptr<SNDFILE, decltype(&sf_close)> snd(sf_open(filename, SFM_READ, &info), &sf_close);
                if (!snd)
                        throw std::runtime_error("sf_open()");

                if (info.channels != 2)
                        throw std::runtime_error("info.channels != 2");

                buffer = std::make_unique<float []>(info.frames * info.channels);
                if (sf_readf_float(snd.get(), buffer.get(), info.frames) != info.frames)
                        throw std::runtime_error("sf_readf_float()");
        }
};