27 |
#include <signal.h> |
#include <signal.h> |
28 |
#include <pthread.h> |
#include <pthread.h> |
29 |
|
|
|
#include <string> |
|
|
|
|
30 |
#include "global.h" |
#include "global.h" |
|
#include "audioio.h" |
|
31 |
#include "diskthread.h" |
#include "diskthread.h" |
32 |
#include "audiothread.h" |
#include "audiothread.h" |
33 |
|
#include "alsaio.h" |
34 |
|
#include "jackio.h" |
35 |
#include "midiin.h" |
#include "midiin.h" |
36 |
#include "stream.h" |
#include "stream.h" |
37 |
#include "RIFF.h" |
#include "RIFF.h" |
59 |
double volume; |
double volume; |
60 |
int num_fragments; |
int num_fragments; |
61 |
int fragmentsize; |
int fragmentsize; |
62 |
std::string input_client; |
String input_client; |
63 |
|
String alsaout; |
64 |
|
String jack_playback[2]; |
65 |
|
bool use_jack; |
66 |
pthread_t signalhandlerthread; |
pthread_t signalhandlerthread; |
67 |
|
uint samplerate; |
68 |
|
|
69 |
void parse_options(int argc, char **argv); |
void parse_options(int argc, char **argv); |
70 |
void signal_handler(int signal); |
void signal_handler(int signal); |
83 |
num_fragments = AUDIO_FRAGMENTS; |
num_fragments = AUDIO_FRAGMENTS; |
84 |
fragmentsize = AUDIO_FRAGMENTSIZE; |
fragmentsize = AUDIO_FRAGMENTSIZE; |
85 |
volume = 0.25; // default volume |
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 |
// parse and assign command line options |
93 |
parse_options(argc, argv); |
parse_options(argc, argv); |
98 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
99 |
} |
} |
100 |
|
|
101 |
dmsg(1,("Initializing audio output...")); |
int error = 1; |
102 |
pAudioIO = new AudioIO(); |
#if HAVE_JACK |
103 |
int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize); |
if (use_jack) { |
104 |
if (error) return EXIT_FAILURE; |
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")); |
dmsg(1,("OK\n")); |
117 |
|
|
118 |
// Loading gig file |
// Loading gig file |
139 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
140 |
} |
} |
141 |
|
|
142 |
DiskThread* pDiskThread = new DiskThread(((pAudioIO->FragmentSize << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo |
DiskThread* pDiskThread = new DiskThread(((pAudioIO->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo |
143 |
AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); |
AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); |
144 |
MidiIn* pMidiInThread = new MidiIn(pAudioThread); |
MidiIn* pMidiInThread = new MidiIn(pAudioThread); |
145 |
|
|
154 |
sleep(1); |
sleep(1); |
155 |
dmsg(1,("Starting audio thread...")); |
dmsg(1,("Starting audio thread...")); |
156 |
pAudioThread->Volume = volume; |
pAudioThread->Volume = volume; |
157 |
pAudioThread->StartThread(); |
pAudioIO->AssignEngine(pAudioThread); |
158 |
|
pAudioIO->Activate(); |
159 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
160 |
|
|
161 |
printf("LinuxSampler initialization completed.\n"); |
printf("LinuxSampler initialization completed.\n"); |
174 |
void signal_handler(int signal) { |
void signal_handler(int signal) { |
175 |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
176 |
// stop all threads |
// stop all threads |
177 |
|
if (pAudioIO) pAudioIO->Close(); |
178 |
if (pMidiInThread) pMidiInThread->StopThread(); |
if (pMidiInThread) pMidiInThread->StopThread(); |
|
if (pAudioThread) pAudioThread->StopThread(); |
|
179 |
if (pDiskThread) pDiskThread->StopThread(); |
if (pDiskThread) pDiskThread->StopThread(); |
180 |
|
|
181 |
// free all resources |
// free all resources |
203 |
{"gig",0,0,0}, |
{"gig",0,0,0}, |
204 |
{"instrument",1,0,0}, |
{"instrument",1,0,0}, |
205 |
{"inputclient",1,0,0}, |
{"inputclient",1,0,0}, |
206 |
|
{"alsaout",1,0,0}, |
207 |
|
{"jackout",1,0,0}, |
208 |
|
{"samplerate",1,0,0}, |
209 |
{"help",0,0,0}, |
{"help",0,0,0}, |
210 |
{0,0,0,0} |
{0,0,0,0} |
211 |
}; |
}; |
215 |
if(res == -1) break; |
if(res == -1) break; |
216 |
if (res == 0) { |
if (res == 0) { |
217 |
switch(option_index) { |
switch(option_index) { |
218 |
case 0: |
case 0: // --numfragments |
219 |
num_fragments = atoi(optarg); |
num_fragments = atoi(optarg); |
220 |
break; |
break; |
221 |
case 1: |
case 1: // --fragmentsize |
222 |
fragmentsize = atoi(optarg); |
fragmentsize = atoi(optarg); |
223 |
break; |
break; |
224 |
case 2: |
case 2: // --volume |
225 |
volume = atof(optarg); |
volume = atof(optarg); |
226 |
break; |
break; |
227 |
case 3: |
case 3: // --dls |
228 |
patch_format = patch_format_dls; |
patch_format = patch_format_dls; |
229 |
break; |
break; |
230 |
case 4: |
case 4: // --gig |
231 |
patch_format = patch_format_gig; |
patch_format = patch_format_gig; |
232 |
break; |
break; |
233 |
case 5: |
case 5: // --instrument |
234 |
instrument_index = atoi(optarg); |
instrument_index = atoi(optarg); |
235 |
break; |
break; |
236 |
case 6: |
case 6: // --inputclient |
237 |
input_client = optarg; |
input_client = optarg; |
238 |
break; |
break; |
239 |
case 7: |
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"); |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
268 |
printf("--gig loads a Gigasampler instrument\n"); |
printf("--gig loads a Gigasampler instrument\n"); |
269 |
printf("--dls loads a DLS instrument\n"); |
printf("--dls loads a DLS instrument\n"); |
276 |
printf(" default: 0.25)\n"); |
printf(" default: 0.25)\n"); |
277 |
printf("--inputclient connects to an Alsa sequencer input client on startup\n"); |
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"); |
printf(" (e.g. 64:0 to connect to a client with ID 64 and port 0)\n"); |
279 |
exit(0); |
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; |
break; |
288 |
} |
} |
289 |
} |
} |