| using namespace std; | |
| void AudioFile::loadWav(const std::string& filename) { | |
| SF_INFO sfinfo; | |
| memset(&sfinfo, 0, sizeof(sfinfo)); | |
| SNDFILE *infile = sf_open(filename.c_str(), SFM_READ, &sfinfo); | |
| if (!infile) { | |
| throw std::runtime_error("Error opening input file: " + std::string(sf_strerror(NULL))); | |
| } | |
| channels_ = sfinfo.channels; | |
| sampleRate_ = sfinfo.samplerate; | |
| sf_count_t frames = sfinfo.frames; | |
| samples_.resize(frames * channels_); | |
| sf_count_t readcount = sf_read_float(infile, samples_.data(), frames * channels_); | |
| if (readcount != frames * channels_) { | |
| cerr << "Warning: short read of audio data." << endl; | |
| } | |
| sf_close(infile); | |
| } | |
| void AudioFile::writeWav(const std::string& filename) { | |
| SF_INFO sfinfo; | |
| memset(&sfinfo, 0, sizeof(sfinfo)); | |
| sfinfo.channels = channels_; | |
| sfinfo.samplerate = sampleRate_; | |
| sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT; | |
| SNDFILE *outfile = sf_open(filename.c_str(), SFM_WRITE, &sfinfo); | |
| if (!outfile) { | |
| throw std::runtime_error("Error opening output file: " + std::string(sf_strerror(NULL))); | |
| } | |
| sf_count_t written = sf_write_float(outfile, samples_.data(), (sf_count_t)samples_.size()); | |
| if (written != (sf_count_t)samples_.size()) { | |
| cerr << "Warning: short write." << endl; | |
| } | |
| sf_close(outfile); | |
| } | |
| AudioFile::AudioFile(const std::string& filename) { | |
| loadWav(filename); | |
| } | |
| void AudioFile::save(const std::string& filename) { | |
| writeWav(filename); | |
| } | |
| float* AudioFile::data() { | |
| return samples_.data(); | |
| } | |
| size_t AudioFile::size() const { | |
| return samples_.size(); | |
| } | |
| int AudioFile::getSampleRate() const { | |
| return sampleRate_; | |
| } | |
| int AudioFile::getChannels() const { | |
| return channels_; | |
| } |
Xet Storage Details
- Size:
- 1.89 kB
- Xet hash:
- f05ac75e5be5a5694507c8ba160f7eda42993f9f9d5c96d81087c377eefe32c6
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.