2 |
* * |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* * |
* * |
7 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
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 "Sampler.h" |
27 |
#include "audioio.h" |
#include "drivers/midi/MidiInputDeviceFactory.h" |
28 |
#include "diskthread.h" |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
29 |
#include "audiothread.h" |
#include "network/lscpserver.h" |
|
#include "midiin.h" |
|
|
#include "stream.h" |
|
|
#include "RIFF.h" |
|
|
#include "gig.h" |
|
30 |
|
|
31 |
|
#if 0 |
32 |
#define AUDIO_CHANNELS 2 // stereo |
#define AUDIO_CHANNELS 2 // stereo |
33 |
#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 |
34 |
#define AUDIO_FRAGMENTSIZE 512 // each fragment has 512 frames |
#define AUDIO_FRAGMENTSIZE 512 // each fragment has 512 frames |
35 |
#define AUDIO_SAMPLERATE 44100 // Hz |
#define AUDIO_SAMPLERATE 44100 // Hz |
36 |
|
#endif |
37 |
|
|
38 |
enum patch_format_t { |
using namespace LinuxSampler; |
39 |
|
|
40 |
|
/*enum patch_format_t { |
41 |
patch_format_unknown, |
patch_format_unknown, |
42 |
patch_format_gig, |
patch_format_gig, |
43 |
patch_format_dls |
patch_format_dls |
44 |
} patch_format; |
} patch_format = patch_format_unknown;*/ |
45 |
|
|
46 |
|
Sampler* pSampler = NULL; |
47 |
|
LSCPServer* pLSCPServer = NULL; |
48 |
|
pthread_t signalhandlerthread; |
49 |
|
/*AudioThread* pEngine = NULL; |
50 |
|
uint instrument_index = 0; |
51 |
|
double volume = 0.25; |
52 |
|
int num_fragments = AUDIO_FRAGMENTS; |
53 |
|
int fragmentsize = AUDIO_FRAGMENTSIZE; |
54 |
|
uint samplerate = AUDIO_SAMPLERATE; |
55 |
|
String input_client; |
56 |
|
String alsaout = "0,0"; // default card |
57 |
|
String jack_playback[2] = { "", "" }; |
58 |
|
bool use_jack = true; |
59 |
|
bool run_server = false;*/ |
60 |
|
|
|
AudioIO* pAudioIO; |
|
|
DiskThread* pDiskThread; |
|
|
AudioThread* pAudioThread; |
|
|
MidiIn* pMidiInThread; |
|
|
RIFF::File* pRIFF; |
|
|
gig::File* pGig; |
|
|
gig::Instrument* pInstrument; |
|
|
int num_fragments; |
|
|
int fragmentsize; |
|
|
pthread_t signalhandlerthread; |
|
61 |
|
|
62 |
void parse_options(int argc, char **argv); |
void parse_options(int argc, char **argv); |
63 |
void signal_handler(int signal); |
void signal_handler(int signal); |
64 |
|
|
65 |
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
|
pAudioIO = NULL; |
|
|
pRIFF = NULL; |
|
|
pGig = NULL; |
|
66 |
|
|
67 |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
68 |
signalhandlerthread = pthread_self(); |
signalhandlerthread = pthread_self(); |
69 |
signal(SIGINT, signal_handler); |
signal(SIGINT, signal_handler); |
70 |
|
|
|
patch_format = patch_format_unknown; |
|
|
num_fragments = AUDIO_FRAGMENTS; |
|
|
fragmentsize = AUDIO_FRAGMENTSIZE; |
|
|
|
|
71 |
// parse and assign command line options |
// parse and assign command line options |
72 |
parse_options(argc, argv); |
//parse_options(argc, argv); |
73 |
|
|
74 |
if (patch_format != patch_format_gig) { |
/*if (patch_format != patch_format_gig) { |
75 |
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"); |
76 |
|
printf("Use 'linuxsampler --help' to see all available options.\n"); |
77 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
78 |
} |
}*/ |
79 |
|
|
80 |
dmsg(1,("Initializing audio output...")); |
dmsg(1,("LinuxSampler %s\n", VERSION)); |
81 |
pAudioIO = new AudioIO(); |
dmsg(1,("Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck\n")); |
82 |
int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize); |
|
83 |
if (error) return EXIT_FAILURE; |
// create LinuxSampler instance |
84 |
|
dmsg(1,("Creating Sampler...")); |
85 |
|
pSampler = new Sampler; |
86 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
87 |
|
|
88 |
// Loading gig file |
dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str())); |
89 |
try { |
dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str())); |
90 |
printf("Loading gig file..."); |
|
91 |
fflush(stdout); |
// create an audio output device |
92 |
pRIFF = new RIFF::File(argv[argc - 1]); |
/* bool no_jack = true; |
93 |
pGig = new gig::File(pRIFF); |
#if HAVE_JACK |
94 |
pInstrument = pGig->GetFirstInstrument(); |
if (use_jack) { |
95 |
pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part |
dmsg(1,("Creating audio output device (Jack)...")); |
96 |
printf("OK\n"); |
try { |
97 |
fflush(stdout); |
pSampler->CreateAudioOutputDevice(audio_output_type_jack); |
98 |
} |
no_jack = false; |
99 |
catch (RIFF::Exception e) { |
} |
100 |
e.PrintMessage(); |
catch (AudioOutputException aoe) { |
101 |
return EXIT_FAILURE; |
aoe.PrintMessage(); |
102 |
|
dmsg(1,("Trying to create Alsa output device instead.\n")); |
103 |
|
} |
104 |
} |
} |
105 |
catch (...) { |
#endif // HAVE_JACK |
106 |
printf("Unknown exception while trying to parse gig file.\n"); |
if (no_jack) { |
107 |
return EXIT_FAILURE; |
dmsg(1,("Creating audio output device (Alsa)...")); |
108 |
|
try { |
109 |
|
pSampler->CreateAudioOutputDevice(audio_output_type_alsa); |
110 |
|
} |
111 |
|
catch (AudioOutputException aoe) { |
112 |
|
aoe.PrintMessage(); |
113 |
|
dmsg(1,("Trying to create Alsa output device instead.\n")); |
114 |
|
return EXIT_FAILURE; |
115 |
|
} |
116 |
} |
} |
117 |
|
dmsg(1,("OK\n"));*/ |
118 |
|
|
119 |
DiskThread* pDiskThread = new DiskThread(((pAudioIO->FragmentSize << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo |
// start LSCP network server |
120 |
AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); |
dmsg(1,("Starting LSCP network server...")); |
121 |
MidiIn* pMidiInThread = new MidiIn(pAudioThread); |
pLSCPServer = new LSCPServer(pSampler); |
122 |
|
pLSCPServer->StartThread(); |
|
dmsg(1,("Starting disk thread...")); |
|
|
pDiskThread->StartThread(); |
|
|
dmsg(1,("OK\n")); |
|
|
dmsg(1,("Starting MIDI in thread...")); |
|
|
pMidiInThread->StartThread(); |
|
|
dmsg(1,("OK\n")); |
|
|
|
|
|
sleep(1); |
|
|
dmsg(1,("Starting audio thread...")); |
|
|
pAudioThread->StartThread(); |
|
123 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
124 |
|
|
125 |
printf("LinuxSampler initialization completed.\n"); |
printf("LinuxSampler initialization completed.\n"); |
126 |
|
|
127 |
while(true) { |
while(true) { |
128 |
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", |
129 |
pAudioThread->ActiveVoiceCount, pAudioThread->ActiveVoiceCountMax, |
pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax, |
130 |
pDiskThread->ActiveStreamCount, pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); |
pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); |
131 |
fflush(stdout); |
fflush(stdout);*/ |
132 |
usleep(500000); |
usleep(500000); |
133 |
} |
} |
134 |
|
|
136 |
} |
} |
137 |
|
|
138 |
void signal_handler(int signal) { |
void signal_handler(int signal) { |
139 |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
140 |
// stop all threads |
if (pLSCPServer) { |
141 |
if (pMidiInThread) pMidiInThread->StopThread(); |
pLSCPServer->StopThread(); |
142 |
if (pAudioThread) pAudioThread->StopThread(); |
delete pLSCPServer; |
143 |
if (pDiskThread) pDiskThread->StopThread(); |
} |
144 |
|
if (pSampler) delete pSampler; |
|
// free all resources |
|
|
if (pMidiInThread) delete pMidiInThread; |
|
|
if (pAudioThread) delete pAudioThread; |
|
|
if (pDiskThread) delete pDiskThread; |
|
|
if (pGig) delete pGig; |
|
|
if (pRIFF) delete pRIFF; |
|
|
if (pAudioIO) delete pAudioIO; |
|
|
|
|
145 |
printf("LinuxSampler stopped due to SIGINT\n"); |
printf("LinuxSampler stopped due to SIGINT\n"); |
146 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
147 |
} |
} |
148 |
} |
} |
149 |
|
|
150 |
void parse_options(int argc, char **argv) { |
/*void parse_options(int argc, char **argv) { |
151 |
int res; |
int res; |
152 |
int option_index = 0; |
int option_index = 0; |
153 |
static struct option long_options[] = |
static struct option long_options[] = |
154 |
{ |
{ |
155 |
{"numfragments",1,0,0}, |
{"numfragments",1,0,0}, |
156 |
{"fragmentsize",1,0,0}, |
{"fragmentsize",1,0,0}, |
157 |
|
{"volume",1,0,0}, |
158 |
{"dls",0,0,0}, |
{"dls",0,0,0}, |
159 |
{"gig",0,0,0}, |
{"gig",0,0,0}, |
160 |
|
{"instrument",1,0,0}, |
161 |
|
{"inputclient",1,0,0}, |
162 |
|
{"alsaout",1,0,0}, |
163 |
|
{"jackout",1,0,0}, |
164 |
|
{"samplerate",1,0,0}, |
165 |
|
{"server",0,0,0}, |
166 |
{"help",0,0,0}, |
{"help",0,0,0}, |
167 |
{0,0,0,0} |
{0,0,0,0} |
168 |
}; |
}; |
172 |
if(res == -1) break; |
if(res == -1) break; |
173 |
if (res == 0) { |
if (res == 0) { |
174 |
switch(option_index) { |
switch(option_index) { |
175 |
case 0: |
case 0: // --numfragments |
176 |
num_fragments = atoi(optarg); |
num_fragments = atoi(optarg); |
177 |
break; |
break; |
178 |
case 1: |
case 1: // --fragmentsize |
179 |
fragmentsize = atoi(optarg); |
fragmentsize = atoi(optarg); |
180 |
break; |
break; |
181 |
case 2: |
case 2: // --volume |
182 |
|
volume = atof(optarg); |
183 |
|
break; |
184 |
|
case 3: // --dls |
185 |
patch_format = patch_format_dls; |
patch_format = patch_format_dls; |
186 |
break; |
break; |
187 |
case 3: |
case 4: // --gig |
188 |
patch_format = patch_format_gig; |
patch_format = patch_format_gig; |
189 |
break; |
break; |
190 |
case 4: |
case 5: // --instrument |
191 |
|
instrument_index = atoi(optarg); |
192 |
|
break; |
193 |
|
case 6: // --inputclient |
194 |
|
input_client = optarg; |
195 |
|
break; |
196 |
|
case 7: // --alsaout |
197 |
|
alsaout = optarg; |
198 |
|
use_jack = false; // If this option is specified do not connect to jack |
199 |
|
break; |
200 |
|
case 8: { // --jackout |
201 |
|
try { |
202 |
|
String arg(optarg); |
203 |
|
// remove outer apostrophes |
204 |
|
arg = arg.substr(arg.find('\'') + 1, arg.rfind('\'') - (arg.find('\'') + 1)); |
205 |
|
// split in two arguments |
206 |
|
jack_playback[0] = arg.substr(0, arg.find("\' ")); |
207 |
|
jack_playback[1] = arg.substr(arg.find("\' ") + 2, arg.size() - (arg.find("\' ") + 2)); |
208 |
|
// remove inner apostrophes |
209 |
|
jack_playback[0] = jack_playback[0].substr(0, jack_playback[0].find('\'')); |
210 |
|
jack_playback[1] = jack_playback[1].substr(jack_playback[1].find('\'') + 1, jack_playback[1].size() - jack_playback[1].find('\'')); |
211 |
|
// this is the default but set it up anyway in case alsa_card was also used. |
212 |
|
use_jack = true; |
213 |
|
} |
214 |
|
catch (...) { |
215 |
|
fprintf(stderr, "Invalid argument '%s' for parameter --jackout\n", optarg); |
216 |
|
exit(EXIT_FAILURE); |
217 |
|
} |
218 |
|
break; |
219 |
|
} |
220 |
|
case 9: // --samplerate |
221 |
|
samplerate = atoi(optarg); |
222 |
|
break; |
223 |
|
case 10: // --server |
224 |
|
run_server = true; |
225 |
|
break; |
226 |
|
case 11: // --help |
227 |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
228 |
|
printf("--gig loads a Gigasampler instrument\n"); |
229 |
|
printf("--dls loads a DLS instrument\n"); |
230 |
|
printf("--instrument index of the instrument in the instrument file if it\n"); |
231 |
|
printf(" contains more than one (default: 0)\n"); |
232 |
printf("--numfragments sets the number of audio fragments\n"); |
printf("--numfragments sets the number of audio fragments\n"); |
233 |
printf("--fragmentsize sets the fragment size\n"); |
printf("--fragmentsize sets the fragment size\n"); |
234 |
printf("--dls loads a DLS instrument\n"); |
printf("--volume sets global volume gain factor (a value > 1.0 means\n"); |
235 |
printf("--gig loads a Gigasampler instrument\n"); |
printf(" amplification, a value < 1.0 means attenuation,\n"); |
236 |
exit(0); |
printf(" default: 0.25)\n"); |
237 |
|
printf("--inputclient connects to an Alsa sequencer input client on startup\n"); |
238 |
|
printf(" (e.g. 64:0 to connect to a client with ID 64 and port 0)\n"); |
239 |
|
printf("--alsaout connects to the given Alsa sound device on startup\n"); |
240 |
|
printf(" (e.g. 0,0 to connect to hw:0,0 or plughw:0,0)\n"); |
241 |
|
printf("--jackout connects to the given Jack playback ports on startup\n"); |
242 |
|
printf(" (e.g. \"\'alsa_pcm:playback_1\' \'alsa_pcm:playback_2\'\"\n"); |
243 |
|
printf(" in case of stereo output)\n"); |
244 |
|
printf("--samplerate sets sample rate if supported by audio output system\n"); |
245 |
|
printf(" (e.g. 44100)\n"); |
246 |
|
printf("--server launch network server for remote control\n"); |
247 |
|
exit(EXIT_SUCCESS); |
248 |
break; |
break; |
249 |
} |
} |
250 |
} |
} |
251 |
} |
} |
252 |
} |
}*/ |