-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSndfileDecoder.cpp
More file actions
73 lines (66 loc) · 2 KB
/
SndfileDecoder.cpp
File metadata and controls
73 lines (66 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// ./SndfileDecoder.cpp
// libsndfileを使用したデコーダーの実装
#include "SndfileDecoder.h"
#include <iostream>
// --- コンストラクタ ---
// メンバ変数を初期化
SndfileDecoder::SndfileDecoder() : sndfile_(nullptr) {
// sfinfo_構造体をゼロで初期化
sfinfo_.frames = 0;
sfinfo_.samplerate = 0;
sfinfo_.channels = 0;
sfinfo_.format = 0;
sfinfo_.sections = 0;
sfinfo_.seekable = 0;
}
// --- デストラクタ ---
// ファイルが開かれていれば閉じる
SndfileDecoder::~SndfileDecoder() {
if (sndfile_) {
sf_close(sndfile_);
}
}
// --- open ---
// 音声ファイルを開く
bool SndfileDecoder::open(const std::string& filePath) {
if (sndfile_) {
sf_close(sndfile_);
}
// sf_open関数でファイルを開き、結果をメンバ変数に格納
sndfile_ = sf_open(filePath.c_str(), SFM_READ, &sfinfo_);
if (!sndfile_) {
std::cerr << "SndfileDecoder Error: Could not open file " << filePath
<< " with libsndfile. Reason: " << sf_strerror(nullptr) << std::endl;
return false;
}
return true;
}
// --- getInfo ---
// 音声ファイルの情報を取得して返す
AudioInfo SndfileDecoder::getInfo() const {
AudioInfo info;
if (sndfile_) {
info.channels = sfinfo_.channels;
info.sampleRate = sfinfo_.samplerate;
info.totalFrames = sfinfo_.frames;
}
return info;
}
// --- read ---
// 指定されたフレーム数の音声データを読み込む
size_t SndfileDecoder::read(float* buffer, size_t frames) {
if (!sndfile_) {
return 0;
}
// libsndfileのsf_readf_float関数で読み込み
return static_cast<size_t>(sf_readf_float(sndfile_, buffer, frames));
}
// --- seek ---
// 指定されたフレーム位置に移動する
bool SndfileDecoder::seek(long long frame) {
if (!sndfile_) {
return false;
}
// libsndfileのsf_seek関数でシーク
return sf_seek(sndfile_, frame, SEEK_SET) != -1;
}