-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSound.h
More file actions
153 lines (127 loc) · 4.01 KB
/
Sound.h
File metadata and controls
153 lines (127 loc) · 4.01 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Sound.h
*
* Created on: Apr 12, 2020
* Author: ans
*/
#ifndef SOUND_H_
#define SOUND_H_
#pragma once
#include <soundio/soundio.h>
#include <atomic> // std::atomic, std::memory_order
#include <functional> // std::function
#include <limits> // std::numeric_limits
#include <stdexcept> // std::runtime_error
#include <string> // std::string, std::to_string
#include <thread> // std::thread
#include <vector> // std::vector
class Sound {
public:
using OutputFunction = std::function<double(unsigned int, double)>;
using WriteFunction = std::function<void(void *, double)>;
static constexpr double epsilon = 0.0001;
enum Channel {
CHANNEL_NONE,
CHANNEL_FRONT_LEFT,
CHANNEL_FRONT_LEFT_CENTER,
CHANNEL_FRONT_RIGHT,
CHANNEL_FRONT_RIGHT_CENTER,
CHANNEL_FRONT_CENTER,
CHANNEL_BACK_LEFT,
CHANNEL_BACK_RIGHT,
CHANNEL_BACK_CENTER,
CHANNEL_SIDE_LEFT,
CHANNEL_SIDE_RIGHT,
CHANNEL_TOP_CENTER,
CHANNEL_TOP_FRONT_LEFT,
CHANNEL_TOP_FRONT_RIGHT,
CHANNEL_TOP_FRONT_CENTER,
CHANNEL_TOP_BACK_LEFT,
CHANNEL_TOP_BACK_RIGHT,
CHANNEL_TOP_BACK_CENTER,
CHANNEL_SUBWOOFER,
CHANNEL_OTHER
};
struct Device {
int index;
std::string id;
std::string name;
Device() : index(-1) {}
Device(int _index, std::string _id, std::string _name)
: index(_index), id(_id), name(_name) {}
};
Sound();
virtual ~Sound();
// general getters
std::vector<Device> listOutputDevices() const;
int getDefaultOutputDeviceIndex() const;
std::string getOutputDeviceId() const;
std::string getOutputDeviceName() const;
std::string getBackend() const;
// setters
void setOutputDeviceByIndex(unsigned int index);
void setOutputDeviceById(const std::string& id);
void setOutputFunction(OutputFunction callBack);
void setOutputStreamName(const std::string& streamName);
void setOutputSampleRate(unsigned int sampleRate);
void setOutputChannels(unsigned int channels);
void setOutputMaxFrames(unsigned int maxFrames);
void setOutputLatency(double latency);
void start(double startTimeInSeconds);
void stop();
// getters for when after the sound system has been started
bool isStarted() const;
int getOutputSampleRate() const;
int getOutputChannels() const;
double getOutputLatency() const;
std::string getOutputLayoutName() const;
Channel getOutputChannelType(unsigned int channel) const;
std::string getOutputChannelName(unsigned int channel) const;
bool isOutputUnderflowOccured();
bool isOutputWritingErrorsOccured(std::string& lastErrorOut);
// getter only for the sound thread
double getTimePosition() const;
private:
void thread();
void threadInit();
void threadClear();
void onDevicesChanged();
void onBackendDisconnected(int error);
void onWrite(int frameCountMin, int frameCountMax);
void onUnderflow();
static void callbackDevicesChanged(SoundIo * soundIo);
static void callbackBackendDisconnected(SoundIo * soundIo, int error);
static void callbackWrite(SoundIoOutStream * soundIoOutStream, int frameCountMin, int frameCountMax);
static void callbackUnderflow(SoundIoOutStream * soundIoOutStream);
int outputDeviceIdToIndex(const std::string& id);
void setOutputDevice(int index);
const SoundIoChannelLayout& getLayout() const;
static void writeSampleS16(void * target, double sample);
static void writeSampleS32(void * target, double sample);
static void writeSampleF32(void * target, double sample);
static void writeSampleF64(void * target, double sample);
bool started;
bool connected;
std::atomic<bool> initialized;
std::atomic<bool> running;
std::thread soundThread;
double secondsOffset;
double secondsPerFrame;
SoundIo * soundIo;
SoundIoDevice * soundIoOutputDevice;
SoundIoOutStream * soundIoOutStream;
int defaultOutputDeviceIndex;
int outputDeviceIndex;
std::string outputDeviceId;
std::string outputDeviceName;
std::string outputStreamName;
int outputSampleRate;
unsigned int outputChannels;
int outputMaxFrames;
double outputLatency;
OutputFunction output;
WriteFunction write;
std::atomic<bool> isUnderflow;
std::atomic<int> lastWritingError;
};
#endif /* SOUND_H_ */