-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathaudiofile.cpp
More file actions
122 lines (95 loc) · 3.02 KB
/
audiofile.cpp
File metadata and controls
122 lines (95 loc) · 3.02 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <fea/audio/audiofile.hpp>
#include <fea/audio/audiofilenotfoundexception.hpp>
#include <fea/assert.hpp>
#include <vorbis/vorbisfile.h>
namespace fea
{
AudioFile::AudioFile() : mFile(nullptr), mChannelCount(0), mSampleRate(0), mSampleAmount(0)
{
}
AudioFile::AudioFile(AudioFile&& other) : mFile(nullptr), mChannelCount(0), mSampleRate(0), mSampleAmount(0)
{
std::swap(mFile, other.mFile);
std::swap(mChannelCount, other.mChannelCount);
std::swap(mSampleRate, other.mSampleRate);
std::swap(mSampleAmount, other.mSampleAmount);
}
AudioFile& AudioFile::operator=(AudioFile&& other)
{
std::swap(mFile, other.mFile);
std::swap(mChannelCount, other.mChannelCount);
std::swap(mSampleRate, other.mSampleRate);
std::swap(mSampleAmount, other.mSampleAmount);
return *this;
}
AudioFile::~AudioFile()
{
if(mFile)
{
ov_clear(mFile);
delete mFile;
}
}
int32_t AudioFile::getChannelCount() const
{
return mChannelCount;
}
int32_t AudioFile::getSampleRate() const
{
return mSampleRate;
}
std::vector<int16_t> AudioFile::getSampleData() const
{
if(mSampleAmount == 0)
return std::vector<int16_t>();
std::vector<int16_t> sampleData(mSampleAmount);
ov_raw_seek(mFile, 0);
int64_t read = 0;
int64_t totalRead = 0;
int bitstream = 0;
while((read = ov_read(mFile, ((char*)sampleData.data()) + totalRead, (mSampleAmount * 2) - (int32_t)totalRead, 0, 2, 1, &bitstream)))
{
if(read < 0)
{
break;
}
totalRead += read;
}
sampleData.resize((size_t)totalRead / 2);
return sampleData;
}
void AudioFile::open(const std::string& path)
{
if(mFile)
{
ov_clear(mFile);
delete mFile;
}
mFile = new OggVorbis_File;
if(ov_fopen(path.c_str(), mFile))
{
throw AudioFileNotFoundException("Error when trying to load audio file '" + path + "!");
}
vorbis_info* info = ov_info(mFile, -1);
mChannelCount = info->channels;
mSampleRate = info->rate;
mSampleAmount = 2 * (size_t)ov_pcm_total(mFile, -1);
}
void AudioFile::fillBufferFromIndex(std::vector<int16_t>& buffer, size_t sampleIndex)
{
FEA_ASSERT(mFile != nullptr, "Cannot fill buffer when no valid file is loaded!");
ov_pcm_seek(mFile, sampleIndex / mChannelCount);
int64_t read = 0;
int64_t totalRead = 0;
int bitstream = 0;
while ((read = ov_read(mFile, ((char*)buffer.data()) + (int32_t)totalRead, (buffer.size() * 2) - (int32_t)totalRead, 0, 2, 1, &bitstream)))
{
if(read < 0)
{
break;
}
totalRead += read;
}
buffer.resize((int32_t)totalRead / 2);
}
}