-
Notifications
You must be signed in to change notification settings - Fork 3
/
Audio.h
73 lines (68 loc) · 2.09 KB
/
Audio.h
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
#pragma once
#include <cstdio>
typedef void (*callbackFunc)(unsigned char *stream, int len);
// WAV file header structure
// should be 1-byte aligned
#pragma pack(1)
struct WAVHEADER {
char riff[4];
unsigned int rLen;
char WAVEfmt[8];
unsigned int fLen; /* 0x1020 */;
unsigned short wFormatTag; /* 0x0001 */
unsigned short nChannels; /* 0x0001 */
unsigned int nSamplesPerSec;
unsigned int nAvgBytesPerSec; // nSamplesPerSec*nChannels*(nBitsPerSample%8)
unsigned short nBlockAlign; /* 0x0001 */
unsigned short nBitsPerSample; /* 0x0008 */
char datastr[4];
unsigned int cbSize;
};
#pragma pack()
class Audio {
public:
Audio(unsigned int sampleFrq_) : bufferLength(4096), sampleFrq(sampleFrq_) { // 2048
recording = false;
wavFileHandle = 0;
}
virtual ~Audio() {};
virtual void play() = 0;
virtual void pause() = 0;
virtual void stop() = 0;
virtual void sleep(unsigned int msec) = 0;
virtual void flush() {
unsigned int msec = (unsigned int)(1000.f * double(bufferLength)/double(sampleFrq) + 1);
sleep(msec);
}
virtual void lock() = 0;
virtual void unlock() = 0;
virtual void setSampleRate(unsigned int newSampleRate) {
sampleFrq = newSampleRate;
}
unsigned int getLatency() {
unsigned int msec = (unsigned int)(1000.f * double(bufferLength)/double(sampleFrq) + 1);
return msec;
}
unsigned int getSampleRate() { return sampleFrq; }
bool isPaused() { return paused; };
virtual bool createWav(const char *fileName);
virtual void closeWav();
static bool dumpWavData(FILE *fp, unsigned char *buffer, unsigned int length);
static short getLastSample() { return lastSample; };
static short* getLastBuffer(size_t& size);
static void stopUsingLastBuffer();
protected:
unsigned int bufferLength;
static bool paused;
static callbackFunc callback;
static void audioCallback(void *userData, unsigned char *stream, int len);
static short *ringBuffer;
static size_t ringBufferSize;
static size_t ringBufferIndex;
static bool recording;
static FILE *wavFileHandle;
static size_t wavDataLength;
static short lastSample;
private:
unsigned int sampleFrq;
};