libsndfile

From vegard.wiki
Revision as of 07:23, 15 December 2019 by Vegard (talk | contribs) (new page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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()");
        }
};