Debian Codegraph @

src/audio/pipe.cpp

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 #include <string.h>
21 #include "../ta-log.h"
22 #include "pipe.h"
23
24 void taPipeThread(void* inst) {
25 TAAudioPipe* in=(TAAudioPipe*)inst;
26 in->runThread();
27 }
28
29 void TAAudioPipe::runThread() {
30 while (running) {
31 onProcess((unsigned char*)sbuf,desc.bufsize);
32 }
33 }
34
35 void TAAudioPipe::onProcess(unsigned char* buf, int nframes) {
36 if (audioProcCallback!=NULL) {
37 if (midiIn!=NULL) midiIn->gather();
38 audioProcCallback(audioProcCallbackUser,inBufs,outBufs,desc.inChans,desc.outChans,desc.bufsize);
39 }
40 short* sb=(short*)buf;
41
42 if (sb==NULL) return;
43
44 for (size_t j=0; j<desc.bufsize; j++) {
45 for (size_t i=0; i<desc.outChans; i++) {
46 if (outBufs[i][j]<-1.0) outBufs[i][j]=-1.0;
47 if (outBufs[i][j]>1.0) outBufs[i][j]=1.0;
48 sb[j*desc.outChans+i]=outBufs[i][j]*32767.0;
49 }
50 }
51
52 fwrite(sb,2,desc.bufsize*desc.outChans,stdout);
53 fflush(stdout);
54 }
55
56 void* TAAudioPipe::getContext() {
57 return NULL;
58 }
59
60 bool TAAudioPipe::quit() {
61 if (!initialized) return false;
62
63 if (running) {
64 running=false;
65 if (outThread) {
66 outThread->join();
67 delete outThread;
68 outThread=NULL;
69 }
70 }
71
72 for (int i=0; i<desc.outChans; i++) {
73 delete[] outBufs[i];
74 }
75
76 delete[] outBufs;
77
78 if (sbuf) {
79 delete[] sbuf;
80 sbuf=NULL;
81 }
82
83 initialized=false;
84 return true;
85 }
86
87 bool TAAudioPipe::setRun(bool run) {
88 if (!initialized) return false;
89
90 if (running!=run) {
91 running=run;
92 if (running) {
93 outThread=new std::thread(taPipeThread,this);
94 } else if (outThread) {
95 outThread->join();
96 delete outThread;
97 outThread=NULL;
98 }
99 }
100
101 return running;
102 }
103
104 std::vector<String> TAAudioPipe::listAudioDevices() {
105 std::vector<String> ret;
106
107 ret.push_back("stdout");
108
109 return ret;
110 }
111
112 bool TAAudioPipe::init(TAAudioDesc& request, TAAudioDesc& response) {
113 if (initialized) {
114 logE("audio already initialized");
115 return false;
116 }
117
118 desc=request;
119 desc.outFormat=TA_AUDIO_FORMAT_F32;
120 response=desc;
121
122 logV("opening stdout for audio...");
123
124 if (desc.outChans>0) {
125 outBufs=new float*[desc.outChans];
126 for (int i=0; i<desc.outChans; i++) {
127 outBufs[i]=new float[desc.bufsize];
128 }
129
130 sbuf=new short[desc.bufsize*desc.outChans];
131 } else {
132 sbuf=NULL;
133 }
134
135 response=desc;
136 initialized=true;
137 return true;
138 }