mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 14:30:36 +00:00
added rate() and channels() to pcm writer interface
This commit is contained in:
parent
d0419241ea
commit
5270069422
2 changed files with 20 additions and 8 deletions
2
pcm.hh
2
pcm.hh
|
|
@ -15,6 +15,8 @@ struct WritePCM
|
|||
virtual void write(TYPE *, int, int = 1) = 0;
|
||||
virtual bool good() = 0;
|
||||
virtual void silence(int) = 0;
|
||||
virtual int rate() = 0;
|
||||
virtual int channels() = 0;
|
||||
};
|
||||
|
||||
template <typename TYPE>
|
||||
|
|
|
|||
26
wav.hh
26
wav.hh
|
|
@ -134,7 +134,7 @@ template <typename TYPE>
|
|||
class WriteWAV : public WritePCM<TYPE>
|
||||
{
|
||||
std::ofstream os;
|
||||
int bytes, channels;
|
||||
int bytes, channels_, rate_;
|
||||
int offset, factor, min, max;
|
||||
void writeLE(int v, int b)
|
||||
{
|
||||
|
|
@ -142,7 +142,9 @@ class WriteWAV : public WritePCM<TYPE>
|
|||
os.put(255 & (v >> (8 * i)));
|
||||
}
|
||||
public:
|
||||
WriteWAV(const char *name, int rate, int bits, int channels) : os(name, std::ios::binary | std::ios::trunc), bytes(bits / 8), channels(channels)
|
||||
WriteWAV(const char *name, int rate, int bits, int channels) :
|
||||
os(name, std::ios::binary | std::ios::trunc),
|
||||
bytes(bits / 8), channels_(channels), rate_(rate)
|
||||
{
|
||||
switch (bits) {
|
||||
case 8:
|
||||
|
|
@ -183,10 +185,10 @@ public:
|
|||
os.write("fmt ", 4); // Subchunk1ID
|
||||
writeLE(16, 4); // Subchunk1Size
|
||||
writeLE(1, 2); // AudioFormat
|
||||
writeLE(channels, 2); // NumChannels
|
||||
writeLE(rate, 4); // SampleRate
|
||||
writeLE(rate * channels * bytes, 4); // ByteRate
|
||||
writeLE(channels * bytes, 2); // BlockAlign
|
||||
writeLE(channels_, 2); // NumChannels
|
||||
writeLE(rate_, 4); // SampleRate
|
||||
writeLE(rate_ * channels_ * bytes, 4); // ByteRate
|
||||
writeLE(channels_ * bytes, 2); // BlockAlign
|
||||
writeLE(8 * bytes, 2); // BitsPerSample
|
||||
os.write("data", 4); // Subchunk2ID
|
||||
writeLE(0, 4); // Subchunk2Size
|
||||
|
|
@ -202,7 +204,7 @@ public:
|
|||
void write(TYPE *buf, int num, int stride = 1)
|
||||
{
|
||||
for (int n = 0; n < num; ++n) {
|
||||
for (int c = 0; c < channels; ++c) {
|
||||
for (int c = 0; c < channels_; ++c) {
|
||||
TYPE v = TYPE(offset) + TYPE(factor) * buf[stride * n + c];
|
||||
writeLE(std::nearbyint(std::clamp(v, TYPE(min), TYPE(max))), bytes);
|
||||
}
|
||||
|
|
@ -214,9 +216,17 @@ public:
|
|||
}
|
||||
void silence(int num)
|
||||
{
|
||||
for (int i = 0; i < num * channels; ++i)
|
||||
for (int i = 0; i < num * channels_; ++i)
|
||||
writeLE(offset, bytes);
|
||||
}
|
||||
int channels()
|
||||
{
|
||||
return channels_;
|
||||
}
|
||||
int rate()
|
||||
{
|
||||
return rate_;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue