25 |
#include <unistd.h> |
#include <unistd.h> |
26 |
#include <getopt.h> |
#include <getopt.h> |
27 |
#include <signal.h> |
#include <signal.h> |
28 |
|
#include <pthread.h> |
29 |
|
|
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" |
38 |
#include "gig.h" |
#include "gig.h" |
39 |
|
#include "network/lscpserver.h" |
40 |
|
|
41 |
#define AUDIO_CHANNELS 2 // stereo |
#define AUDIO_CHANNELS 2 // stereo |
42 |
#define AUDIO_FRAGMENTS 3 // 3 fragments, if it does not work set it to 2 |
#define AUDIO_FRAGMENTS 3 // 3 fragments, if it does not work set it to 2 |
47 |
patch_format_unknown, |
patch_format_unknown, |
48 |
patch_format_gig, |
patch_format_gig, |
49 |
patch_format_dls |
patch_format_dls |
50 |
} patch_format; |
} patch_format = patch_format_unknown; |
51 |
|
|
52 |
AudioIO* pAudioIO; |
AudioIO* pAudioIO = NULL; |
53 |
DiskThread* pDiskThread; |
MidiIn* pMidiInThread = NULL; |
54 |
AudioThread* pAudioThread; |
LSCPServer* pLSCPServer = NULL; |
55 |
MidiIn* pMidiInThread; |
AudioThread* pEngine = NULL; |
56 |
|
uint instrument_index = 0; |
57 |
RIFF::File* pRIFF; |
double volume = 0.25; |
58 |
gig::File* pGig; |
int num_fragments = AUDIO_FRAGMENTS; |
59 |
gig::Instrument* pInstrument; |
int fragmentsize = AUDIO_FRAGMENTSIZE; |
60 |
|
uint samplerate = AUDIO_SAMPLERATE; |
61 |
|
String input_client; |
62 |
|
String alsaout = "0,0"; // default card |
63 |
|
String jack_playback[2] = { "", "" }; |
64 |
|
bool use_jack = true; |
65 |
|
bool run_server = false; |
66 |
|
pthread_t signalhandlerthread; |
67 |
|
|
68 |
void parse_options(int argc, char **argv); |
void parse_options(int argc, char **argv); |
69 |
void signal_handler(int signal); |
void signal_handler(int signal); |
70 |
|
|
|
int midi_non_blocking; |
|
|
int num_fragments; |
|
|
int fragmentsize; |
|
|
bool instrument_is_DLS; |
|
|
bool instruemtn_is_gig; |
|
|
char midi_device[40]; |
|
|
|
|
71 |
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
|
pAudioIO = NULL; |
|
|
pRIFF = NULL; |
|
|
pGig = NULL; |
|
72 |
|
|
73 |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
74 |
|
signalhandlerthread = pthread_self(); |
75 |
signal(SIGINT, signal_handler); |
signal(SIGINT, signal_handler); |
76 |
|
|
|
patch_format = patch_format_unknown; |
|
|
midi_non_blocking = 1; |
|
|
num_fragments = AUDIO_FRAGMENTS; |
|
|
fragmentsize = AUDIO_FRAGMENTSIZE; |
|
|
strcpy(midi_device, "/dev/midi00"); |
|
|
|
|
77 |
// parse and assign command line options |
// parse and assign command line options |
78 |
parse_options(argc, argv); |
parse_options(argc, argv); |
79 |
|
|
80 |
if (patch_format != patch_format_gig) { |
if (patch_format != patch_format_gig) { |
81 |
printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig\n"); |
printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n"); |
82 |
printf("to load a .gig file!\n"); |
printf("Use 'linuxsampler --help' to see all available options.\n"); |
83 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
84 |
} |
} |
85 |
|
|
86 |
dmsg(("Initializing audio output...")); |
int error = 1; |
87 |
pAudioIO = new AudioIO(); |
#if HAVE_JACK |
88 |
int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize); |
if (use_jack) { |
89 |
if (error) return EXIT_FAILURE; |
dmsg(1,("Initializing audio output (Jack)...")); |
90 |
dmsg(("OK\n")); |
pAudioIO = new JackIO(); |
91 |
|
error = ((JackIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, jack_playback); |
92 |
|
if (error) dmsg(1,("Trying Alsa output instead.\n")); |
93 |
|
} |
94 |
|
#endif // HAVE_JACK |
95 |
|
if (error) { |
96 |
|
dmsg(1,("Initializing audio output (Alsa)...")); |
97 |
|
pAudioIO = new AlsaIO(); |
98 |
|
int error = ((AlsaIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, samplerate, num_fragments, fragmentsize, alsaout); |
99 |
|
if (error) return EXIT_FAILURE; |
100 |
|
} |
101 |
|
dmsg(1,("OK\n")); |
102 |
|
|
103 |
|
AudioThread* pEngine = new AudioThread(pAudioIO); |
104 |
|
MidiIn* pMidiInThread = new MidiIn(pEngine); |
105 |
|
|
106 |
// Loading gig file |
// Loading gig file |
107 |
try { |
result_t result = pEngine->LoadInstrument(argv[argc - 1], instrument_index); |
108 |
printf("Loading gig file..."); |
if (result.type == result_type_error) return EXIT_FAILURE; |
109 |
fflush(stdout); |
pEngine->Volume = volume; |
|
pRIFF = new RIFF::File(argv[argc - 1]); |
|
|
pGig = new gig::File(pRIFF); |
|
|
pInstrument = pGig->GetFirstInstrument(); |
|
|
pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part |
|
|
printf("OK\n"); |
|
|
fflush(stdout); |
|
|
} |
|
|
catch (RIFF::Exception e) { |
|
|
e.PrintMessage(); |
|
|
return EXIT_FAILURE; |
|
|
} |
|
|
catch (...) { |
|
|
printf("Unknown exception while trying to parse gig file.\n"); |
|
|
return EXIT_FAILURE; |
|
|
} |
|
110 |
|
|
111 |
DiskThread* pDiskThread = new DiskThread(((pAudioIO->FragmentSize << MAX_PITCH) << 1) + 3); //FIXME: assuming stereo |
dmsg(1,("Starting MIDI in thread...")); |
112 |
AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); |
if (input_client.size() > 0) pMidiInThread->SubscribeToClient(input_client.c_str()); |
|
MidiIn* pMidiInThread = new MidiIn(pAudioThread); |
|
|
|
|
|
dmsg(("Starting disk thread...")); |
|
|
pDiskThread->StartThread(); |
|
|
dmsg(("OK\n")); |
|
|
dmsg(("Starting MIDI in thread...")); |
|
113 |
pMidiInThread->StartThread(); |
pMidiInThread->StartThread(); |
114 |
dmsg(("OK\n")); |
dmsg(1,("OK\n")); |
115 |
|
|
116 |
sleep(1); |
sleep(1); |
117 |
dmsg(("Starting audio thread...")); |
|
118 |
pAudioThread->StartThread(); |
dmsg(1,("Starting audio thread...")); |
119 |
dmsg(("OK\n")); |
pAudioIO->AssignEngine(pEngine); |
120 |
|
pAudioIO->Activate(); |
121 |
|
dmsg(1,("OK\n")); |
122 |
|
|
123 |
|
if (run_server) { |
124 |
|
dmsg(1,("Starting network server...")); |
125 |
|
pLSCPServer = new LSCPServer(pEngine); |
126 |
|
pLSCPServer->StartThread(); |
127 |
|
dmsg(1,("OK\n")); |
128 |
|
} |
129 |
|
|
130 |
printf("LinuxSampler initialization completed.\n"); |
printf("LinuxSampler initialization completed.\n"); |
131 |
|
|
132 |
while(true) { |
while(true) { |
133 |
printf("Voices: %3.3d Streams: %3.3d \r",pAudioThread->ActiveVoiceCount, pDiskThread->ActiveStreamCount); fflush(stdout); |
printf("Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d, Unused: %3.3d)\r", |
134 |
|
pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax, |
135 |
|
pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); |
136 |
|
fflush(stdout); |
137 |
usleep(500000); |
usleep(500000); |
138 |
} |
} |
139 |
|
|
141 |
} |
} |
142 |
|
|
143 |
void signal_handler(int signal) { |
void signal_handler(int signal) { |
144 |
if (signal == SIGINT) { |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
145 |
// stop all threads |
// stop all threads |
146 |
|
if (pAudioIO) pAudioIO->Close(); |
147 |
if (pMidiInThread) pMidiInThread->StopThread(); |
if (pMidiInThread) pMidiInThread->StopThread(); |
|
if (pAudioThread) pAudioThread->StopThread(); |
|
|
if (pDiskThread) pDiskThread->StopThread(); |
|
|
|
|
|
sleep(1); |
|
148 |
|
|
149 |
// free all resources |
// free all resources |
150 |
if (pMidiInThread) delete pMidiInThread; |
if (pMidiInThread) delete pMidiInThread; |
151 |
if (pAudioThread) delete pAudioThread; |
if (pEngine) delete pEngine; |
|
if (pDiskThread) delete pDiskThread; |
|
|
if (pGig) delete pGig; |
|
|
if (pRIFF) delete pRIFF; |
|
152 |
if (pAudioIO) delete pAudioIO; |
if (pAudioIO) delete pAudioIO; |
153 |
|
|
154 |
printf("LinuxSampler stopped due to SIGINT\n"); |
printf("LinuxSampler stopped due to SIGINT\n"); |
163 |
{ |
{ |
164 |
{"numfragments",1,0,0}, |
{"numfragments",1,0,0}, |
165 |
{"fragmentsize",1,0,0}, |
{"fragmentsize",1,0,0}, |
166 |
|
{"volume",1,0,0}, |
167 |
{"dls",0,0,0}, |
{"dls",0,0,0}, |
168 |
{"gig",0,0,0}, |
{"gig",0,0,0}, |
169 |
|
{"instrument",1,0,0}, |
170 |
|
{"inputclient",1,0,0}, |
171 |
|
{"alsaout",1,0,0}, |
172 |
|
{"jackout",1,0,0}, |
173 |
|
{"samplerate",1,0,0}, |
174 |
|
{"server",0,0,0}, |
175 |
{"help",0,0,0}, |
{"help",0,0,0}, |
176 |
{0,0,0,0} |
{0,0,0,0} |
177 |
}; |
}; |
181 |
if(res == -1) break; |
if(res == -1) break; |
182 |
if (res == 0) { |
if (res == 0) { |
183 |
switch(option_index) { |
switch(option_index) { |
184 |
case 0: |
case 0: // --numfragments |
185 |
num_fragments = atoi(optarg); |
num_fragments = atoi(optarg); |
186 |
break; |
break; |
187 |
case 1: |
case 1: // --fragmentsize |
188 |
fragmentsize = atoi(optarg); |
fragmentsize = atoi(optarg); |
189 |
break; |
break; |
190 |
case 2: |
case 2: // --volume |
191 |
|
volume = atof(optarg); |
192 |
|
break; |
193 |
|
case 3: // --dls |
194 |
patch_format = patch_format_dls; |
patch_format = patch_format_dls; |
195 |
break; |
break; |
196 |
case 3: |
case 4: // --gig |
197 |
patch_format = patch_format_gig; |
patch_format = patch_format_gig; |
198 |
break; |
break; |
199 |
case 4: |
case 5: // --instrument |
200 |
|
instrument_index = atoi(optarg); |
201 |
|
break; |
202 |
|
case 6: // --inputclient |
203 |
|
input_client = optarg; |
204 |
|
break; |
205 |
|
case 7: // --alsaout |
206 |
|
alsaout = optarg; |
207 |
|
use_jack = false; // If this option is specified do not connect to jack |
208 |
|
break; |
209 |
|
case 8: { // --jackout |
210 |
|
try { |
211 |
|
String arg(optarg); |
212 |
|
// remove outer apostrophes |
213 |
|
arg = arg.substr(arg.find('\'') + 1, arg.rfind('\'') - (arg.find('\'') + 1)); |
214 |
|
// split in two arguments |
215 |
|
jack_playback[0] = arg.substr(0, arg.find("\' ")); |
216 |
|
jack_playback[1] = arg.substr(arg.find("\' ") + 2, arg.size() - (arg.find("\' ") + 2)); |
217 |
|
// remove inner apostrophes |
218 |
|
jack_playback[0] = jack_playback[0].substr(0, jack_playback[0].find('\'')); |
219 |
|
jack_playback[1] = jack_playback[1].substr(jack_playback[1].find('\'') + 1, jack_playback[1].size() - jack_playback[1].find('\'')); |
220 |
|
// this is the default but set it up anyway in case alsa_card was also used. |
221 |
|
use_jack = true; |
222 |
|
} |
223 |
|
catch (...) { |
224 |
|
fprintf(stderr, "Invalid argument '%s' for parameter --jackout\n", optarg); |
225 |
|
exit(EXIT_FAILURE); |
226 |
|
} |
227 |
|
break; |
228 |
|
} |
229 |
|
case 9: // --samplerate |
230 |
|
samplerate = atoi(optarg); |
231 |
|
break; |
232 |
|
case 10: // --server |
233 |
|
run_server = true; |
234 |
|
break; |
235 |
|
case 11: // --help |
236 |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
237 |
|
printf("--gig loads a Gigasampler instrument\n"); |
238 |
|
printf("--dls loads a DLS instrument\n"); |
239 |
|
printf("--instrument index of the instrument in the instrument file if it\n"); |
240 |
|
printf(" contains more than one (default: 0)\n"); |
241 |
printf("--numfragments sets the number of audio fragments\n"); |
printf("--numfragments sets the number of audio fragments\n"); |
242 |
printf("--fragmentsize sets the fragment size\n"); |
printf("--fragmentsize sets the fragment size\n"); |
243 |
printf("--dls loads a DLS instrument\n"); |
printf("--volume sets global volume gain factor (a value > 1.0 means\n"); |
244 |
printf("--gig loads a Gigasampler instrument\n"); |
printf(" amplification, a value < 1.0 means attenuation,\n"); |
245 |
exit(0); |
printf(" default: 0.25)\n"); |
246 |
|
printf("--inputclient connects to an Alsa sequencer input client on startup\n"); |
247 |
|
printf(" (e.g. 64:0 to connect to a client with ID 64 and port 0)\n"); |
248 |
|
printf("--alsaout connects to the given Alsa sound device on startup\n"); |
249 |
|
printf(" (e.g. 0,0 to connect to hw:0,0 or plughw:0,0)\n"); |
250 |
|
printf("--jackout connects to the given Jack playback ports on startup\n"); |
251 |
|
printf(" (e.g. \"\'alsa_pcm:playback_1\' \'alsa_pcm:playback_2\'\"\n"); |
252 |
|
printf(" in case of stereo output)\n"); |
253 |
|
printf("--samplerate sets sample rate if supported by audio output system\n"); |
254 |
|
printf(" (e.g. 44100)\n"); |
255 |
|
printf("--server launch network server for remote control\n"); |
256 |
|
exit(EXIT_SUCCESS); |
257 |
break; |
break; |
258 |
} |
} |
259 |
} |
} |