1 |
schoenebeck |
9 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
|
|
* Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * |
6 |
|
|
* * |
7 |
|
|
* This program is free software; you can redistribute it and/or modify * |
8 |
|
|
* it under the terms of the GNU General Public License as published by * |
9 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
10 |
|
|
* (at your option) any later version. * |
11 |
|
|
* * |
12 |
|
|
* This program is distributed in the hope that it will be useful, * |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 |
|
|
* GNU General Public License for more details. * |
16 |
|
|
* * |
17 |
|
|
* You should have received a copy of the GNU General Public License * |
18 |
|
|
* along with this program; if not, write to the Free Software * |
19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
20 |
|
|
* MA 02111-1307 USA * |
21 |
|
|
***************************************************************************/ |
22 |
|
|
|
23 |
|
|
#include <stdio.h> |
24 |
|
|
#include <stdlib.h> |
25 |
|
|
#include <unistd.h> |
26 |
|
|
#include <getopt.h> |
27 |
|
|
#include <signal.h> |
28 |
schoenebeck |
13 |
#include <pthread.h> |
29 |
schoenebeck |
9 |
|
30 |
|
|
#include "global.h" |
31 |
|
|
#include "diskthread.h" |
32 |
|
|
#include "audiothread.h" |
33 |
schoenebeck |
31 |
#include "alsaio.h" |
34 |
|
|
#include "jackio.h" |
35 |
schoenebeck |
9 |
#include "midiin.h" |
36 |
|
|
#include "stream.h" |
37 |
|
|
#include "RIFF.h" |
38 |
|
|
#include "gig.h" |
39 |
|
|
|
40 |
|
|
#define AUDIO_CHANNELS 2 // stereo |
41 |
|
|
#define AUDIO_FRAGMENTS 3 // 3 fragments, if it does not work set it to 2 |
42 |
|
|
#define AUDIO_FRAGMENTSIZE 512 // each fragment has 512 frames |
43 |
|
|
#define AUDIO_SAMPLERATE 44100 // Hz |
44 |
|
|
|
45 |
|
|
enum patch_format_t { |
46 |
|
|
patch_format_unknown, |
47 |
|
|
patch_format_gig, |
48 |
|
|
patch_format_dls |
49 |
|
|
} patch_format; |
50 |
|
|
|
51 |
|
|
AudioIO* pAudioIO; |
52 |
|
|
DiskThread* pDiskThread; |
53 |
|
|
AudioThread* pAudioThread; |
54 |
|
|
MidiIn* pMidiInThread; |
55 |
|
|
RIFF::File* pRIFF; |
56 |
|
|
gig::File* pGig; |
57 |
|
|
gig::Instrument* pInstrument; |
58 |
schoenebeck |
20 |
uint instrument_index; |
59 |
|
|
double volume; |
60 |
schoenebeck |
12 |
int num_fragments; |
61 |
|
|
int fragmentsize; |
62 |
schoenebeck |
33 |
String input_client; |
63 |
|
|
String alsaout; |
64 |
|
|
String jack_playback[2]; |
65 |
|
|
bool use_jack; |
66 |
schoenebeck |
13 |
pthread_t signalhandlerthread; |
67 |
schoenebeck |
33 |
uint samplerate; |
68 |
schoenebeck |
9 |
|
69 |
|
|
void parse_options(int argc, char **argv); |
70 |
|
|
void signal_handler(int signal); |
71 |
|
|
|
72 |
|
|
int main(int argc, char **argv) { |
73 |
|
|
pAudioIO = NULL; |
74 |
|
|
pRIFF = NULL; |
75 |
|
|
pGig = NULL; |
76 |
|
|
|
77 |
|
|
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
78 |
schoenebeck |
13 |
signalhandlerthread = pthread_self(); |
79 |
schoenebeck |
9 |
signal(SIGINT, signal_handler); |
80 |
|
|
|
81 |
|
|
patch_format = patch_format_unknown; |
82 |
schoenebeck |
20 |
instrument_index = 0; |
83 |
schoenebeck |
9 |
num_fragments = AUDIO_FRAGMENTS; |
84 |
|
|
fragmentsize = AUDIO_FRAGMENTSIZE; |
85 |
schoenebeck |
20 |
volume = 0.25; // default volume |
86 |
schoenebeck |
33 |
alsaout = "0,0"; // default card |
87 |
|
|
jack_playback[0] = ""; |
88 |
|
|
jack_playback[1] = ""; |
89 |
|
|
samplerate = AUDIO_SAMPLERATE; |
90 |
|
|
use_jack = true; |
91 |
schoenebeck |
9 |
|
92 |
|
|
// parse and assign command line options |
93 |
|
|
parse_options(argc, argv); |
94 |
|
|
|
95 |
|
|
if (patch_format != patch_format_gig) { |
96 |
schoenebeck |
12 |
printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n"); |
97 |
schoenebeck |
20 |
printf("Use 'linuxsampler --help' to see all available options.\n"); |
98 |
schoenebeck |
9 |
return EXIT_FAILURE; |
99 |
|
|
} |
100 |
|
|
|
101 |
schoenebeck |
33 |
int error = 1; |
102 |
schoenebeck |
31 |
#if HAVE_JACK |
103 |
schoenebeck |
33 |
if (use_jack) { |
104 |
|
|
dmsg(1,("Initializing audio output (Jack)...")); |
105 |
|
|
pAudioIO = new JackIO(); |
106 |
|
|
error = ((JackIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, jack_playback); |
107 |
|
|
if (error) dmsg(1,("Trying Alsa output instead.\n")); |
108 |
|
|
} |
109 |
|
|
#endif // HAVE_JACK |
110 |
schoenebeck |
31 |
if (error) { |
111 |
|
|
dmsg(1,("Initializing audio output (Alsa)...")); |
112 |
|
|
pAudioIO = new AlsaIO(); |
113 |
schoenebeck |
33 |
int error = ((AlsaIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, samplerate, num_fragments, fragmentsize, alsaout); |
114 |
schoenebeck |
31 |
if (error) return EXIT_FAILURE; |
115 |
|
|
} |
116 |
|
|
dmsg(1,("OK\n")); |
117 |
schoenebeck |
9 |
|
118 |
|
|
// Loading gig file |
119 |
|
|
try { |
120 |
|
|
printf("Loading gig file..."); |
121 |
|
|
fflush(stdout); |
122 |
|
|
pRIFF = new RIFF::File(argv[argc - 1]); |
123 |
|
|
pGig = new gig::File(pRIFF); |
124 |
schoenebeck |
20 |
pInstrument = pGig->GetInstrument(instrument_index); |
125 |
|
|
if (!pInstrument) { |
126 |
|
|
printf("there's no instrument with index %d.\n", instrument_index); |
127 |
|
|
exit(EXIT_FAILURE); |
128 |
|
|
} |
129 |
schoenebeck |
9 |
pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part |
130 |
|
|
printf("OK\n"); |
131 |
|
|
fflush(stdout); |
132 |
|
|
} |
133 |
|
|
catch (RIFF::Exception e) { |
134 |
|
|
e.PrintMessage(); |
135 |
|
|
return EXIT_FAILURE; |
136 |
|
|
} |
137 |
|
|
catch (...) { |
138 |
|
|
printf("Unknown exception while trying to parse gig file.\n"); |
139 |
|
|
return EXIT_FAILURE; |
140 |
|
|
} |
141 |
|
|
|
142 |
schoenebeck |
31 |
DiskThread* pDiskThread = new DiskThread(((pAudioIO->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo |
143 |
schoenebeck |
9 |
AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); |
144 |
|
|
MidiIn* pMidiInThread = new MidiIn(pAudioThread); |
145 |
|
|
|
146 |
schoenebeck |
12 |
dmsg(1,("Starting disk thread...")); |
147 |
schoenebeck |
9 |
pDiskThread->StartThread(); |
148 |
schoenebeck |
12 |
dmsg(1,("OK\n")); |
149 |
|
|
dmsg(1,("Starting MIDI in thread...")); |
150 |
schoenebeck |
20 |
if (input_client.size() > 0) pMidiInThread->SubscribeToClient(input_client.c_str()); |
151 |
schoenebeck |
9 |
pMidiInThread->StartThread(); |
152 |
schoenebeck |
12 |
dmsg(1,("OK\n")); |
153 |
schoenebeck |
9 |
|
154 |
|
|
sleep(1); |
155 |
schoenebeck |
12 |
dmsg(1,("Starting audio thread...")); |
156 |
schoenebeck |
20 |
pAudioThread->Volume = volume; |
157 |
schoenebeck |
31 |
pAudioIO->AssignEngine(pAudioThread); |
158 |
|
|
pAudioIO->Activate(); |
159 |
schoenebeck |
12 |
dmsg(1,("OK\n")); |
160 |
schoenebeck |
9 |
|
161 |
|
|
printf("LinuxSampler initialization completed.\n"); |
162 |
|
|
|
163 |
senoner |
10 |
while(true) { |
164 |
schoenebeck |
13 |
printf("Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d, Unused: %3.3d)\r", |
165 |
|
|
pAudioThread->ActiveVoiceCount, pAudioThread->ActiveVoiceCountMax, |
166 |
|
|
pDiskThread->ActiveStreamCount, pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); |
167 |
|
|
fflush(stdout); |
168 |
senoner |
10 |
usleep(500000); |
169 |
|
|
} |
170 |
|
|
|
171 |
schoenebeck |
9 |
return EXIT_SUCCESS; |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
void signal_handler(int signal) { |
175 |
schoenebeck |
20 |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
176 |
schoenebeck |
9 |
// stop all threads |
177 |
schoenebeck |
31 |
if (pAudioIO) pAudioIO->Close(); |
178 |
schoenebeck |
9 |
if (pMidiInThread) pMidiInThread->StopThread(); |
179 |
|
|
if (pDiskThread) pDiskThread->StopThread(); |
180 |
|
|
|
181 |
|
|
// free all resources |
182 |
|
|
if (pMidiInThread) delete pMidiInThread; |
183 |
|
|
if (pAudioThread) delete pAudioThread; |
184 |
|
|
if (pDiskThread) delete pDiskThread; |
185 |
|
|
if (pGig) delete pGig; |
186 |
|
|
if (pRIFF) delete pRIFF; |
187 |
|
|
if (pAudioIO) delete pAudioIO; |
188 |
|
|
|
189 |
|
|
printf("LinuxSampler stopped due to SIGINT\n"); |
190 |
|
|
exit(EXIT_SUCCESS); |
191 |
|
|
} |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
void parse_options(int argc, char **argv) { |
195 |
|
|
int res; |
196 |
|
|
int option_index = 0; |
197 |
|
|
static struct option long_options[] = |
198 |
|
|
{ |
199 |
|
|
{"numfragments",1,0,0}, |
200 |
|
|
{"fragmentsize",1,0,0}, |
201 |
schoenebeck |
20 |
{"volume",1,0,0}, |
202 |
schoenebeck |
9 |
{"dls",0,0,0}, |
203 |
|
|
{"gig",0,0,0}, |
204 |
schoenebeck |
20 |
{"instrument",1,0,0}, |
205 |
|
|
{"inputclient",1,0,0}, |
206 |
schoenebeck |
33 |
{"alsaout",1,0,0}, |
207 |
|
|
{"jackout",1,0,0}, |
208 |
|
|
{"samplerate",1,0,0}, |
209 |
schoenebeck |
9 |
{"help",0,0,0}, |
210 |
|
|
{0,0,0,0} |
211 |
|
|
}; |
212 |
|
|
|
213 |
|
|
while (true) { |
214 |
|
|
res = getopt_long_only(argc, argv, "", long_options, &option_index); |
215 |
|
|
if(res == -1) break; |
216 |
|
|
if (res == 0) { |
217 |
|
|
switch(option_index) { |
218 |
schoenebeck |
33 |
case 0: // --numfragments |
219 |
schoenebeck |
9 |
num_fragments = atoi(optarg); |
220 |
|
|
break; |
221 |
schoenebeck |
33 |
case 1: // --fragmentsize |
222 |
schoenebeck |
9 |
fragmentsize = atoi(optarg); |
223 |
|
|
break; |
224 |
schoenebeck |
33 |
case 2: // --volume |
225 |
schoenebeck |
20 |
volume = atof(optarg); |
226 |
|
|
break; |
227 |
schoenebeck |
33 |
case 3: // --dls |
228 |
schoenebeck |
9 |
patch_format = patch_format_dls; |
229 |
|
|
break; |
230 |
schoenebeck |
33 |
case 4: // --gig |
231 |
schoenebeck |
9 |
patch_format = patch_format_gig; |
232 |
|
|
break; |
233 |
schoenebeck |
33 |
case 5: // --instrument |
234 |
schoenebeck |
20 |
instrument_index = atoi(optarg); |
235 |
|
|
break; |
236 |
schoenebeck |
33 |
case 6: // --inputclient |
237 |
schoenebeck |
20 |
input_client = optarg; |
238 |
|
|
break; |
239 |
schoenebeck |
33 |
case 7: // --alsaout |
240 |
|
|
alsaout = optarg; |
241 |
|
|
use_jack = false; // If this option is specified do not connect to jack |
242 |
|
|
break; |
243 |
|
|
case 8: { // --jackout |
244 |
|
|
try { |
245 |
|
|
String arg(optarg); |
246 |
|
|
// remove outer apostrophes |
247 |
|
|
arg = arg.substr(arg.find('\'') + 1, arg.rfind('\'') - (arg.find('\'') + 1)); |
248 |
|
|
// split in two arguments |
249 |
|
|
jack_playback[0] = arg.substr(0, arg.find("\' ")); |
250 |
|
|
jack_playback[1] = arg.substr(arg.find("\' ") + 2, arg.size() - (arg.find("\' ") + 2)); |
251 |
|
|
// remove inner apostrophes |
252 |
|
|
jack_playback[0] = jack_playback[0].substr(0, jack_playback[0].find('\'')); |
253 |
|
|
jack_playback[1] = jack_playback[1].substr(jack_playback[1].find('\'') + 1, jack_playback[1].size() - jack_playback[1].find('\'')); |
254 |
|
|
// this is the default but set it up anyway in case alsa_card was also used. |
255 |
|
|
use_jack = true; |
256 |
|
|
} |
257 |
|
|
catch (...) { |
258 |
|
|
fprintf(stderr, "Invalid argument '%s' for parameter --jackout\n", optarg); |
259 |
|
|
exit(EXIT_FAILURE); |
260 |
|
|
} |
261 |
|
|
break; |
262 |
|
|
} |
263 |
|
|
case 9: // --samplerate |
264 |
|
|
samplerate = atoi(optarg); |
265 |
|
|
break; |
266 |
|
|
case 10: // --help |
267 |
schoenebeck |
9 |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
268 |
schoenebeck |
20 |
printf("--gig loads a Gigasampler instrument\n"); |
269 |
|
|
printf("--dls loads a DLS instrument\n"); |
270 |
|
|
printf("--instrument index of the instrument in the instrument file if it\n"); |
271 |
|
|
printf(" contains more than one (default: 0)\n"); |
272 |
schoenebeck |
9 |
printf("--numfragments sets the number of audio fragments\n"); |
273 |
|
|
printf("--fragmentsize sets the fragment size\n"); |
274 |
schoenebeck |
20 |
printf("--volume sets global volume gain factor (a value > 1.0 means\n"); |
275 |
|
|
printf(" amplification, a value < 1.0 means attenuation,\n"); |
276 |
|
|
printf(" default: 0.25)\n"); |
277 |
|
|
printf("--inputclient connects to an Alsa sequencer input client on startup\n"); |
278 |
|
|
printf(" (e.g. 64:0 to connect to a client with ID 64 and port 0)\n"); |
279 |
schoenebeck |
33 |
printf("--alsaout connects to the given Alsa sound device on startup\n"); |
280 |
|
|
printf(" (e.g. 0,0 to connect to hw:0,0 or plughw:0,0)\n"); |
281 |
|
|
printf("--jackout connects to the given Jack playback ports on startup\n"); |
282 |
|
|
printf(" (e.g. \"\'alsa_pcm:playback_1\' \'alsa_pcm:playback_2\'\"\n"); |
283 |
|
|
printf(" in case of stereo output)\n"); |
284 |
|
|
printf("--samplerate sets sample rate if supported by audio output system\n"); |
285 |
|
|
printf(" (e.g. 44100)\n"); |
286 |
|
|
exit(EXIT_SUCCESS); |
287 |
schoenebeck |
9 |
break; |
288 |
|
|
} |
289 |
|
|
} |
290 |
|
|
} |
291 |
|
|
} |