-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWAVFile.cpp
130 lines (116 loc) · 2.44 KB
/
WAVFile.cpp
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
123
124
125
126
127
128
129
130
#include "WAVFile.h"
WAVFile::WAVFile()
{
}
WAVFile::~WAVFile()
{
}
qint64 WAVFile::pos() const
{
sf_count_t p = sf_seek(m_sndfile, 0, SEEK_CUR);
return p;
}
qint64 WAVFile::size() const
{
sf_count_t p = sf_seek(m_sndfile, 0, SEEK_END);
return p;
}
bool WAVFile::seek(qint64 pos)
{
sf_seek(m_sndfile, pos, SEEK_SET);
return true;
}
bool WAVFile::atEnd() const
{
if (pos() == size())
{
return true;
}
return false;
}
bool WAVFile::reset()
{
sf_count_t p = sf_seek(m_sndfile, 0, SEEK_SET);
if (p == -1)
{
return false;
}
return true;
}
bool WAVFile::open(QString fileName, OpenMode mode, QAudioFormat format)
{
int snd_mode;
switch (mode)
{
case QIODevice::ReadOnly:
snd_mode = SFM_READ;
m_info.channels = 0;
m_info.format = 0;
m_info.frames = 0;
m_info.samplerate = 0;
break;
case QIODevice::WriteOnly:
snd_mode = SFM_WRITE;
m_info.channels = format.channelCount();
m_info.format = SF_FORMAT_WAV;
m_info.frames = 0;
m_info.samplerate = format.sampleRate();
break;
case QIODevice::ReadWrite:
snd_mode = SFM_RDWR;
break;
default:
return false;
}
m_sndfile = sf_open(fileName.toLocal8Bit(), snd_mode, &m_info);
QIODevice::open(mode);
return m_sndfile;
}
qint64 WAVFile::readData(char * data, qint64 maxSize)
{
return sf_read_raw(m_sndfile, data, maxSize);
}
qint64 WAVFile::writeData(const char * data, qint64 maxSize)
{
return sf_write_raw(m_sndfile, data, maxSize);
}
QAudioFormat WAVFile::format()
{
QAudioFormat audioFormat;
audioFormat.setCodec("audio/pcm");
audioFormat.setByteOrder(QAudioFormat::LittleEndian);
audioFormat.setSampleRate(m_info.samplerate);
audioFormat.setChannelCount(m_info.channels);
switch (m_info.format & 0x0f)
{
case SF_FORMAT_PCM_U8:
audioFormat.setSampleSize(8);
audioFormat.setSampleType(QAudioFormat::UnSignedInt);
break;
case SF_FORMAT_PCM_S8:
audioFormat.setSampleSize(8);
audioFormat.setSampleType(QAudioFormat::SignedInt);
break;
case SF_FORMAT_PCM_16:
audioFormat.setSampleSize(16);
audioFormat.setSampleType(QAudioFormat::SignedInt);
break;
case SF_FORMAT_PCM_24:
audioFormat.setSampleSize(24);
audioFormat.setSampleType(QAudioFormat::SignedInt);
break;
case SF_FORMAT_PCM_32:
audioFormat.setSampleSize(32);
audioFormat.setSampleType(QAudioFormat::SignedInt);
break;
default:
audioFormat.setSampleSize(8);
audioFormat.setSampleType(QAudioFormat::UnSignedInt);
break;
}
return audioFormat;
}
void WAVFile::close()
{
sf_close(m_sndfile);
}