27 |
#include <signal.h> |
#include <signal.h> |
28 |
#include <pthread.h> |
#include <pthread.h> |
29 |
|
|
30 |
|
#include <string> |
31 |
|
|
32 |
#include "global.h" |
#include "global.h" |
|
#include "audioio.h" |
|
33 |
#include "diskthread.h" |
#include "diskthread.h" |
34 |
#include "audiothread.h" |
#include "audiothread.h" |
35 |
|
#include "alsaio.h" |
36 |
|
#include "jackio.h" |
37 |
#include "midiin.h" |
#include "midiin.h" |
38 |
#include "stream.h" |
#include "stream.h" |
39 |
#include "RIFF.h" |
#include "RIFF.h" |
57 |
RIFF::File* pRIFF; |
RIFF::File* pRIFF; |
58 |
gig::File* pGig; |
gig::File* pGig; |
59 |
gig::Instrument* pInstrument; |
gig::Instrument* pInstrument; |
60 |
|
uint instrument_index; |
61 |
|
double volume; |
62 |
int num_fragments; |
int num_fragments; |
63 |
int fragmentsize; |
int fragmentsize; |
64 |
|
std::string input_client; |
65 |
pthread_t signalhandlerthread; |
pthread_t signalhandlerthread; |
66 |
|
|
67 |
void parse_options(int argc, char **argv); |
void parse_options(int argc, char **argv); |
77 |
signal(SIGINT, signal_handler); |
signal(SIGINT, signal_handler); |
78 |
|
|
79 |
patch_format = patch_format_unknown; |
patch_format = patch_format_unknown; |
80 |
|
instrument_index = 0; |
81 |
num_fragments = AUDIO_FRAGMENTS; |
num_fragments = AUDIO_FRAGMENTS; |
82 |
fragmentsize = AUDIO_FRAGMENTSIZE; |
fragmentsize = AUDIO_FRAGMENTSIZE; |
83 |
|
volume = 0.25; // default volume |
84 |
|
|
85 |
// parse and assign command line options |
// parse and assign command line options |
86 |
parse_options(argc, argv); |
parse_options(argc, argv); |
87 |
|
|
88 |
if (patch_format != patch_format_gig) { |
if (patch_format != patch_format_gig) { |
89 |
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"); |
90 |
|
printf("Use 'linuxsampler --help' to see all available options.\n"); |
91 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
92 |
} |
} |
93 |
|
|
94 |
dmsg(1,("Initializing audio output...")); |
#if HAVE_JACK |
95 |
pAudioIO = new AudioIO(); |
dmsg(1,("Initializing audio output (Jack)...")); |
96 |
int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize); |
pAudioIO = new JackIO(); |
97 |
|
int error = ((JackIO*)pAudioIO)->Initialize(AUDIO_CHANNELS); |
98 |
|
if (error) { |
99 |
|
dmsg(1,("Trying Alsa output instead.\n")); |
100 |
|
dmsg(1,("Initializing audio output (Alsa)...")); |
101 |
|
pAudioIO = new AlsaIO(); |
102 |
|
int error = ((AlsaIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize); |
103 |
|
if (error) return EXIT_FAILURE; |
104 |
|
} |
105 |
|
dmsg(1,("OK\n")); |
106 |
|
#else // Alsa only |
107 |
|
dmsg(1,("Initializing audio output (Alsa)...")); |
108 |
|
pAudioIO = new AlsaIO(); |
109 |
|
int error = ((AlsaIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize); |
110 |
if (error) return EXIT_FAILURE; |
if (error) return EXIT_FAILURE; |
111 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
112 |
|
#endif // HAVE_JACK |
113 |
|
|
114 |
// Loading gig file |
// Loading gig file |
115 |
try { |
try { |
117 |
fflush(stdout); |
fflush(stdout); |
118 |
pRIFF = new RIFF::File(argv[argc - 1]); |
pRIFF = new RIFF::File(argv[argc - 1]); |
119 |
pGig = new gig::File(pRIFF); |
pGig = new gig::File(pRIFF); |
120 |
pInstrument = pGig->GetFirstInstrument(); |
pInstrument = pGig->GetInstrument(instrument_index); |
121 |
|
if (!pInstrument) { |
122 |
|
printf("there's no instrument with index %d.\n", instrument_index); |
123 |
|
exit(EXIT_FAILURE); |
124 |
|
} |
125 |
pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part |
pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part |
126 |
printf("OK\n"); |
printf("OK\n"); |
127 |
fflush(stdout); |
fflush(stdout); |
135 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
136 |
} |
} |
137 |
|
|
138 |
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 |
139 |
AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); |
AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); |
140 |
MidiIn* pMidiInThread = new MidiIn(pAudioThread); |
MidiIn* pMidiInThread = new MidiIn(pAudioThread); |
141 |
|
|
143 |
pDiskThread->StartThread(); |
pDiskThread->StartThread(); |
144 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
145 |
dmsg(1,("Starting MIDI in thread...")); |
dmsg(1,("Starting MIDI in thread...")); |
146 |
|
if (input_client.size() > 0) pMidiInThread->SubscribeToClient(input_client.c_str()); |
147 |
pMidiInThread->StartThread(); |
pMidiInThread->StartThread(); |
148 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
149 |
|
|
150 |
sleep(1); |
sleep(1); |
151 |
dmsg(1,("Starting audio thread...")); |
dmsg(1,("Starting audio thread...")); |
152 |
pAudioThread->StartThread(); |
pAudioThread->Volume = volume; |
153 |
|
pAudioIO->AssignEngine(pAudioThread); |
154 |
|
pAudioIO->Activate(); |
155 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
156 |
|
|
157 |
printf("LinuxSampler initialization completed.\n"); |
printf("LinuxSampler initialization completed.\n"); |
168 |
} |
} |
169 |
|
|
170 |
void signal_handler(int signal) { |
void signal_handler(int signal) { |
171 |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
172 |
// stop all threads |
// stop all threads |
173 |
|
if (pAudioIO) pAudioIO->Close(); |
174 |
if (pMidiInThread) pMidiInThread->StopThread(); |
if (pMidiInThread) pMidiInThread->StopThread(); |
|
if (pAudioThread) pAudioThread->StopThread(); |
|
175 |
if (pDiskThread) pDiskThread->StopThread(); |
if (pDiskThread) pDiskThread->StopThread(); |
176 |
|
|
177 |
// free all resources |
// free all resources |
194 |
{ |
{ |
195 |
{"numfragments",1,0,0}, |
{"numfragments",1,0,0}, |
196 |
{"fragmentsize",1,0,0}, |
{"fragmentsize",1,0,0}, |
197 |
|
{"volume",1,0,0}, |
198 |
{"dls",0,0,0}, |
{"dls",0,0,0}, |
199 |
{"gig",0,0,0}, |
{"gig",0,0,0}, |
200 |
|
{"instrument",1,0,0}, |
201 |
|
{"inputclient",1,0,0}, |
202 |
{"help",0,0,0}, |
{"help",0,0,0}, |
203 |
{0,0,0,0} |
{0,0,0,0} |
204 |
}; |
}; |
215 |
fragmentsize = atoi(optarg); |
fragmentsize = atoi(optarg); |
216 |
break; |
break; |
217 |
case 2: |
case 2: |
218 |
patch_format = patch_format_dls; |
volume = atof(optarg); |
219 |
break; |
break; |
220 |
case 3: |
case 3: |
221 |
patch_format = patch_format_gig; |
patch_format = patch_format_dls; |
222 |
break; |
break; |
223 |
case 4: |
case 4: |
224 |
|
patch_format = patch_format_gig; |
225 |
|
break; |
226 |
|
case 5: |
227 |
|
instrument_index = atoi(optarg); |
228 |
|
break; |
229 |
|
case 6: |
230 |
|
input_client = optarg; |
231 |
|
break; |
232 |
|
case 7: |
233 |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
234 |
|
printf("--gig loads a Gigasampler instrument\n"); |
235 |
|
printf("--dls loads a DLS instrument\n"); |
236 |
|
printf("--instrument index of the instrument in the instrument file if it\n"); |
237 |
|
printf(" contains more than one (default: 0)\n"); |
238 |
printf("--numfragments sets the number of audio fragments\n"); |
printf("--numfragments sets the number of audio fragments\n"); |
239 |
printf("--fragmentsize sets the fragment size\n"); |
printf("--fragmentsize sets the fragment size\n"); |
240 |
printf("--dls loads a DLS instrument\n"); |
printf("--volume sets global volume gain factor (a value > 1.0 means\n"); |
241 |
printf("--gig loads a Gigasampler instrument\n"); |
printf(" amplification, a value < 1.0 means attenuation,\n"); |
242 |
|
printf(" default: 0.25)\n"); |
243 |
|
printf("--inputclient connects to an Alsa sequencer input client on startup\n"); |
244 |
|
printf(" (e.g. 64:0 to connect to a client with ID 64 and port 0)\n"); |
245 |
exit(0); |
exit(0); |
246 |
break; |
break; |
247 |
} |
} |