1 |
/*************************************************************************** |
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 |
#include <pthread.h> |
29 |
|
30 |
#include "global.h" |
31 |
#include "diskthread.h" |
32 |
#include "audiothread.h" |
33 |
#include "alsaio.h" |
34 |
#include "jackio.h" |
35 |
#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 |
uint instrument_index; |
59 |
double volume; |
60 |
int num_fragments; |
61 |
int fragmentsize; |
62 |
String input_client; |
63 |
String alsaout; |
64 |
String jack_playback[2]; |
65 |
bool use_jack; |
66 |
pthread_t signalhandlerthread; |
67 |
uint samplerate; |
68 |
|
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 |
signalhandlerthread = pthread_self(); |
79 |
signal(SIGINT, signal_handler); |
80 |
|
81 |
patch_format = patch_format_unknown; |
82 |
instrument_index = 0; |
83 |
num_fragments = AUDIO_FRAGMENTS; |
84 |
fragmentsize = AUDIO_FRAGMENTSIZE; |
85 |
volume = 0.25; // default volume |
86 |
alsaout = "0,0"; // default card |
87 |
jack_playback[0] = ""; |
88 |
jack_playback[1] = ""; |
89 |
samplerate = AUDIO_SAMPLERATE; |
90 |
use_jack = true; |
91 |
|
92 |
// parse and assign command line options |
93 |
parse_options(argc, argv); |
94 |
|
95 |
if (patch_format != patch_format_gig) { |
96 |
printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n"); |
97 |
printf("Use 'linuxsampler --help' to see all available options.\n"); |
98 |
return EXIT_FAILURE; |
99 |
} |
100 |
|
101 |
int error = 1; |
102 |
#if HAVE_JACK |
103 |
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 |
if (error) { |
111 |
dmsg(1,("Initializing audio output (Alsa)...")); |
112 |
pAudioIO = new AlsaIO(); |
113 |
int error = ((AlsaIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, samplerate, num_fragments, fragmentsize, alsaout); |
114 |
if (error) return EXIT_FAILURE; |
115 |
} |
116 |
dmsg(1,("OK\n")); |
117 |
|
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 |
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 |
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 |
DiskThread* pDiskThread = new DiskThread(((pAudioIO->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo |
143 |
AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); |
144 |
MidiIn* pMidiInThread = new MidiIn(pAudioThread); |
145 |
|
146 |
dmsg(1,("Starting disk thread...")); |
147 |
pDiskThread->StartThread(); |
148 |
dmsg(1,("OK\n")); |
149 |
dmsg(1,("Starting MIDI in thread...")); |
150 |
if (input_client.size() > 0) pMidiInThread->SubscribeToClient(input_client.c_str()); |
151 |
pMidiInThread->StartThread(); |
152 |
dmsg(1,("OK\n")); |
153 |
|
154 |
sleep(1); |
155 |
dmsg(1,("Starting audio thread...")); |
156 |
pAudioThread->Volume = volume; |
157 |
pAudioIO->AssignEngine(pAudioThread); |
158 |
pAudioIO->Activate(); |
159 |
dmsg(1,("OK\n")); |
160 |
|
161 |
printf("LinuxSampler initialization completed.\n"); |
162 |
|
163 |
while(true) { |
164 |
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 |
usleep(500000); |
169 |
} |
170 |
|
171 |
return EXIT_SUCCESS; |
172 |
} |
173 |
|
174 |
void signal_handler(int signal) { |
175 |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
176 |
// stop all threads |
177 |
if (pAudioIO) pAudioIO->Close(); |
178 |
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 |
{"volume",1,0,0}, |
202 |
{"dls",0,0,0}, |
203 |
{"gig",0,0,0}, |
204 |
{"instrument",1,0,0}, |
205 |
{"inputclient",1,0,0}, |
206 |
{"alsaout",1,0,0}, |
207 |
{"jackout",1,0,0}, |
208 |
{"samplerate",1,0,0}, |
209 |
{"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 |
case 0: // --numfragments |
219 |
num_fragments = atoi(optarg); |
220 |
break; |
221 |
case 1: // --fragmentsize |
222 |
fragmentsize = atoi(optarg); |
223 |
break; |
224 |
case 2: // --volume |
225 |
volume = atof(optarg); |
226 |
break; |
227 |
case 3: // --dls |
228 |
patch_format = patch_format_dls; |
229 |
break; |
230 |
case 4: // --gig |
231 |
patch_format = patch_format_gig; |
232 |
break; |
233 |
case 5: // --instrument |
234 |
instrument_index = atoi(optarg); |
235 |
break; |
236 |
case 6: // --inputclient |
237 |
input_client = optarg; |
238 |
break; |
239 |
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 |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
268 |
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 |
printf("--numfragments sets the number of audio fragments\n"); |
273 |
printf("--fragmentsize sets the fragment size\n"); |
274 |
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 |
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 |
break; |
288 |
} |
289 |
} |
290 |
} |
291 |
} |