Debian Codegraph @

src/audio/taAudio.h

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef _TAAUDIO_H
21 #define _TAAUDIO_H
22 #include "../ta-utils.h"
23 #include <memory>
24 #include "../fixedQueue.h"
25 #include "../pch.h"
26
27 struct SampleRateChangeEvent {
28 double rate;
29 explicit SampleRateChangeEvent(double r):
30 rate(r) {}
31 };
32
33 struct BufferSizeChangeEvent {
34 unsigned int bufsize;
35 explicit BufferSizeChangeEvent(unsigned int bs):
36 bufsize(bs) {}
37 };
38
39 enum TAAudioFormat {
40 TA_AUDIO_FORMAT_F32=0,
41 TA_AUDIO_FORMAT_F64,
42 TA_AUDIO_FORMAT_U8,
43 TA_AUDIO_FORMAT_S8,
44 TA_AUDIO_FORMAT_U16,
45 TA_AUDIO_FORMAT_S16,
46 TA_AUDIO_FORMAT_U32,
47 TA_AUDIO_FORMAT_S32,
48 TA_AUDIO_FORMAT_U16BE,
49 TA_AUDIO_FORMAT_S16BE,
50 TA_AUDIO_FORMAT_U32BE,
51 TA_AUDIO_FORMAT_S32BE
52 };
53
54 struct TAAudioDesc {
55 String name, deviceName;
56 double rate;
57 unsigned int bufsize, fragments;
58 unsigned char inChans, outChans;
59 TAAudioFormat outFormat;
60
61 bool wasapiEx;
62
63 TAAudioDesc():
64 rate(0.0),
65 bufsize(0),
66 fragments(0),
67 inChans(0),
68 outChans(0),
69 outFormat(TA_AUDIO_FORMAT_F32),
70 wasapiEx(false) {}
71 };
72
73
74 enum TAMidiMessageTypes {
75 TA_MIDI_NOTE_OFF=0x80,
76 TA_MIDI_NOTE_ON=0x90,
77 TA_MIDI_AFTERTOUCH=0xa0,
78 TA_MIDI_CONTROL=0xb0,
79 TA_MIDI_PROGRAM=0xc0,
80 TA_MIDI_CHANNEL_AFTERTOUCH=0xd0,
81 TA_MIDI_PITCH_BEND=0xe0,
82 TA_MIDI_SYSEX=0xf0,
83 TA_MIDI_MTC_FRAME=0xf1,
84 TA_MIDI_POSITION=0xf2,
85 TA_MIDI_SONG_SELECT=0xf3,
86 TA_MIDI_TUNE_REQUEST=0xf6,
87 TA_MIDI_SYSEX_END=0xf7,
88 TA_MIDI_CLOCK=0xf8,
89 TA_MIDI_MACHINE_PLAY=0xfa,
90 TA_MIDI_MACHINE_RESUME=0xfb,
91 TA_MIDI_MACHINE_STOP=0xfc,
92 TA_MIDI_KEEPALIVE=0xfe,
93 TA_MIDI_RESET=0xff
94 };
95
96 struct TAMidiMessage {
97 double time;
98 unsigned char type;
99 unsigned char data[7];
100 std::shared_ptr<unsigned char> sysExData;
101 size_t sysExLen;
102
103 void submitSysEx(std::vector<unsigned char> data);
104 void done();
105
106 TAMidiMessage(unsigned char t, unsigned char d0, unsigned char d1):
107 time(0.0),
108 type(t),
109 sysExData(NULL),
110 sysExLen(0) {
111 memset(&data,0,sizeof(data));
112 data[0]=d0;
113 data[1]=d1;
114 }
115
116 TAMidiMessage():
117 time(0.0),
118 type(0),
119 sysExData(NULL),
120 sysExLen(0) {
121 memset(&data,0,sizeof(data));
122 }
123 };
124
125 class TAMidiIn {
126 public:
127 FixedQueue<TAMidiMessage,8192> queue;
128 virtual bool gather();
129 bool next(TAMidiMessage& where);
130 virtual bool isDeviceOpen();
131 virtual bool openDevice(String name);
132 virtual bool closeDevice();
133 virtual std::vector<String> listDevices();
134 virtual bool init();
135 virtual bool quit();
136 TAMidiIn() {
137 }
138 virtual ~TAMidiIn();
139 };
140
141 class TAMidiOut {
142 FixedQueue<TAMidiMessage,8192> queue;
143 public:
144 virtual bool send(const TAMidiMessage& what);
145 virtual bool isDeviceOpen();
146 virtual bool openDevice(String name);
147 virtual bool closeDevice();
148 virtual std::vector<String> listDevices();
149 virtual bool init();
150 virtual bool quit();
151 TAMidiOut() {
152 }
153 virtual ~TAMidiOut();
154 };
155
156 class TAAudio {
157 protected:
158 TAAudioDesc desc;
159 TAAudioFormat outFormat;
160 bool running, initialized;
161 float** inBufs;
162 float** outBufs;
163 void (*audioProcCallback)(void*,float**,float**,int,int,unsigned int);
164 void* audioProcCallbackUser;
165 void (*sampleRateChanged)(SampleRateChangeEvent);
166 void (*bufferSizeChanged)(BufferSizeChangeEvent);
167 public:
168 TAMidiIn* midiIn;
169 TAMidiOut* midiOut;
170 void setSampleRateChangeCallback(void (*callback)(SampleRateChangeEvent));
171 void setBufferSizeChangeCallback(void (*callback)(BufferSizeChangeEvent));
172
173 void setCallback(void (*callback)(void*,float**,float**,int,int,unsigned int), void* user);
174
175 virtual void* getContext();
176 virtual bool quit();
177 virtual bool setRun(bool run);
178 virtual std::vector<String> listAudioDevices();
179 bool initMidi(bool jack);
180 void quitMidi();
181 virtual bool init(TAAudioDesc& request, TAAudioDesc& response);
182
183 TAAudio():
184 outFormat(TA_AUDIO_FORMAT_F32),
185 running(false),
186 initialized(false),
187 inBufs(NULL),
188 outBufs(NULL),
189 audioProcCallback(NULL),
190 audioProcCallbackUser(NULL),
191 sampleRateChanged(NULL),
192 bufferSizeChanged(NULL),
193 midiIn(NULL),
194 midiOut(NULL) {}
195
196 virtual ~TAAudio();
197 };
198 #endif