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