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" |
27 |
#include "diskthread.h" |
#include "diskthread.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 |
uint instrument_index; |
int fragmentsize = AUDIO_FRAGMENTSIZE; |
56 |
double volume; |
uint samplerate = AUDIO_SAMPLERATE; |
57 |
int num_fragments; |
String input_client; |
58 |
int fragmentsize; |
String alsaout = "0,0"; // default card |
59 |
String input_client; |
String jack_playback[2] = { "", "" }; |
60 |
String alsaout; |
bool use_jack = true; |
61 |
String jack_playback[2]; |
bool run_server = false; |
62 |
bool use_jack; |
pthread_t signalhandlerthread; |
|
pthread_t signalhandlerthread; |
|
|
uint samplerate; |
|
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; |
|
|
instrument_index = 0; |
|
|
num_fragments = AUDIO_FRAGMENTS; |
|
|
fragmentsize = AUDIO_FRAGMENTSIZE; |
|
|
volume = 0.25; // default volume |
|
|
alsaout = "0,0"; // default card |
|
|
jack_playback[0] = ""; |
|
|
jack_playback[1] = ""; |
|
|
samplerate = AUDIO_SAMPLERATE; |
|
|
use_jack = true; |
|
|
|
|
73 |
// parse and assign command line options |
// parse and assign command line options |
74 |
parse_options(argc, argv); |
parse_options(argc, argv); |
75 |
|
|
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->GetInstrument(instrument_index); |
|
|
if (!pInstrument) { |
|
|
printf("there's no instrument with index %d.\n", instrument_index); |
|
|
exit(EXIT_FAILURE); |
|
|
} |
|
|
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->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //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()); |
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->Volume = volume; |
pAudioIO->AssignEngine(pEngine); |
|
pAudioIO->AssignEngine(pAudioThread); |
|
116 |
pAudioIO->Activate(); |
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 |
} |
} |
141 |
// stop all threads |
// stop all threads |
142 |
if (pAudioIO) pAudioIO->Close(); |
if (pAudioIO) pAudioIO->Close(); |
143 |
if (pMidiInThread) pMidiInThread->StopThread(); |
if (pMidiInThread) pMidiInThread->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"); |
167 |
{"alsaout",1,0,0}, |
{"alsaout",1,0,0}, |
168 |
{"jackout",1,0,0}, |
{"jackout",1,0,0}, |
169 |
{"samplerate",1,0,0}, |
{"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 |
}; |
}; |
225 |
case 9: // --samplerate |
case 9: // --samplerate |
226 |
samplerate = atoi(optarg); |
samplerate = atoi(optarg); |
227 |
break; |
break; |
228 |
case 10: // --help |
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"); |
printf("--gig loads a Gigasampler instrument\n"); |
234 |
printf("--dls loads a DLS instrument\n"); |
printf("--dls loads a DLS instrument\n"); |
248 |
printf(" in case of stereo output)\n"); |
printf(" in case of stereo output)\n"); |
249 |
printf("--samplerate sets sample rate if supported by audio output system\n"); |
printf("--samplerate sets sample rate if supported by audio output system\n"); |
250 |
printf(" (e.g. 44100)\n"); |
printf(" (e.g. 44100)\n"); |
251 |
|
printf("--server launch network server for remote control\n"); |
252 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
253 |
break; |
break; |
254 |
} |
} |