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> |
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" |
30 |
#include "midiin.h" |
|
31 |
#include "stream.h" |
using namespace LinuxSampler; |
32 |
#include "RIFF.h" |
|
33 |
#include "gig.h" |
Sampler* pSampler = NULL; |
34 |
|
LSCPServer* pLSCPServer = NULL; |
35 |
#define AUDIO_CHANNELS 2 // stereo |
pthread_t signalhandlerthread; |
|
#define AUDIO_FRAGMENTS 3 // 3 fragments, if it does not work set it to 2 |
|
|
#define AUDIO_FRAGMENTSIZE 512 // each fragment has 512 frames |
|
|
#define AUDIO_SAMPLERATE 44100 // Hz |
|
|
|
|
|
enum patch_format_t { |
|
|
patch_format_unknown, |
|
|
patch_format_gig, |
|
|
patch_format_dls |
|
|
} patch_format; |
|
|
|
|
|
AudioIO* pAudioIO; |
|
|
DiskThread* pDiskThread; |
|
|
AudioThread* pAudioThread; |
|
|
MidiIn* pMidiInThread; |
|
|
|
|
|
RIFF::File* pRIFF; |
|
|
gig::File* pGig; |
|
|
gig::Instrument* pInstrument; |
|
36 |
|
|
37 |
void parse_options(int argc, char **argv); |
void parse_options(int argc, char **argv); |
38 |
void signal_handler(int signal); |
void signal_handler(int signal); |
39 |
|
|
|
int midi_non_blocking; |
|
|
int num_fragments; |
|
|
int fragmentsize; |
|
|
bool instrument_is_DLS; |
|
|
bool instruemtn_is_gig; |
|
|
char midi_device[40]; |
|
|
|
|
40 |
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
|
pAudioIO = NULL; |
|
|
pRIFF = NULL; |
|
|
pGig = NULL; |
|
41 |
|
|
42 |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
43 |
|
signalhandlerthread = pthread_self(); |
44 |
signal(SIGINT, signal_handler); |
signal(SIGINT, signal_handler); |
45 |
|
|
|
patch_format = patch_format_unknown; |
|
|
midi_non_blocking = 1; |
|
|
num_fragments = AUDIO_FRAGMENTS; |
|
|
fragmentsize = AUDIO_FRAGMENTSIZE; |
|
|
strcpy(midi_device, "/dev/midi00"); |
|
|
|
|
46 |
// parse and assign command line options |
// parse and assign command line options |
47 |
parse_options(argc, argv); |
//parse_options(argc, argv); |
|
|
|
|
if (patch_format != patch_format_gig) { |
|
|
printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig\n"); |
|
|
printf("to load a .gig file!\n"); |
|
|
return EXIT_FAILURE; |
|
|
} |
|
48 |
|
|
49 |
dmsg(("Initializing audio output...")); |
dmsg(1,("LinuxSampler %s\n", VERSION)); |
50 |
pAudioIO = new AudioIO(); |
dmsg(1,("Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck\n")); |
|
int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize); |
|
|
if (error) return EXIT_FAILURE; |
|
|
dmsg(("OK\n")); |
|
|
|
|
|
// Loading gig file |
|
|
try { |
|
|
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; |
|
|
} |
|
51 |
|
|
52 |
DiskThread* pDiskThread = new DiskThread(((pAudioIO->FragmentSize << MAX_PITCH) << 1) + 3); //FIXME: assuming stereo |
// create LinuxSampler instance |
53 |
AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); |
dmsg(1,("Creating Sampler...")); |
54 |
MidiIn* pMidiInThread = new MidiIn(pAudioThread); |
pSampler = new Sampler; |
55 |
|
dmsg(1,("OK\n")); |
56 |
dmsg(("Starting disk thread...")); |
|
57 |
pDiskThread->StartThread(); |
dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str())); |
58 |
dmsg(("OK\n")); |
dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str())); |
59 |
dmsg(("Starting MIDI in thread...")); |
|
60 |
pMidiInThread->StartThread(); |
// start LSCP network server |
61 |
dmsg(("OK\n")); |
dmsg(1,("Starting LSCP network server (on TCP port %d)...", LSCP_PORT)); |
62 |
|
pLSCPServer = new LSCPServer(pSampler); |
63 |
sleep(1); |
pLSCPServer->StartThread(); |
64 |
dmsg(("Starting audio thread...")); |
pLSCPServer->WaitUntilInitialized(); |
65 |
pAudioThread->StartThread(); |
dmsg(1,("OK\n")); |
|
dmsg(("OK\n")); |
|
66 |
|
|
67 |
printf("LinuxSampler initialization completed.\n"); |
printf("LinuxSampler initialization completed.\n"); |
68 |
|
|
69 |
while(true) sleep(1000); |
while(true) { |
70 |
|
/*printf("Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d, Unused: %3.3d)\r", |
71 |
|
pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax, |
72 |
|
pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); |
73 |
|
fflush(stdout);*/ |
74 |
|
usleep(500000); |
75 |
|
} |
76 |
|
|
77 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
78 |
} |
} |
79 |
|
|
80 |
void signal_handler(int signal) { |
void signal_handler(int signal) { |
81 |
if (signal == SIGINT) { |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
82 |
// stop all threads |
if (pLSCPServer) { |
83 |
if (pMidiInThread) pMidiInThread->StopThread(); |
pLSCPServer->StopThread(); |
84 |
if (pAudioThread) pAudioThread->StopThread(); |
delete pLSCPServer; |
85 |
if (pDiskThread) pDiskThread->StopThread(); |
} |
86 |
|
if (pSampler) delete pSampler; |
|
sleep(1); |
|
|
|
|
|
// 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; |
|
|
|
|
87 |
printf("LinuxSampler stopped due to SIGINT\n"); |
printf("LinuxSampler stopped due to SIGINT\n"); |
88 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
89 |
} |
} |
90 |
} |
} |
91 |
|
|
92 |
void parse_options(int argc, char **argv) { |
/*void parse_options(int argc, char **argv) { |
93 |
int res; |
int res; |
94 |
int option_index = 0; |
int option_index = 0; |
95 |
static struct option long_options[] = |
static struct option long_options[] = |
96 |
{ |
{ |
97 |
{"numfragments",1,0,0}, |
{"numfragments",1,0,0}, |
98 |
{"fragmentsize",1,0,0}, |
{"fragmentsize",1,0,0}, |
99 |
|
{"volume",1,0,0}, |
100 |
{"dls",0,0,0}, |
{"dls",0,0,0}, |
101 |
{"gig",0,0,0}, |
{"gig",0,0,0}, |
102 |
|
{"instrument",1,0,0}, |
103 |
|
{"inputclient",1,0,0}, |
104 |
|
{"alsaout",1,0,0}, |
105 |
|
{"jackout",1,0,0}, |
106 |
|
{"samplerate",1,0,0}, |
107 |
|
{"server",0,0,0}, |
108 |
{"help",0,0,0}, |
{"help",0,0,0}, |
109 |
{0,0,0,0} |
{0,0,0,0} |
110 |
}; |
}; |
114 |
if(res == -1) break; |
if(res == -1) break; |
115 |
if (res == 0) { |
if (res == 0) { |
116 |
switch(option_index) { |
switch(option_index) { |
117 |
case 0: |
case 0: // --numfragments |
118 |
num_fragments = atoi(optarg); |
num_fragments = atoi(optarg); |
119 |
break; |
break; |
120 |
case 1: |
case 1: // --fragmentsize |
121 |
fragmentsize = atoi(optarg); |
fragmentsize = atoi(optarg); |
122 |
break; |
break; |
123 |
case 2: |
case 2: // --volume |
124 |
|
volume = atof(optarg); |
125 |
|
break; |
126 |
|
case 3: // --dls |
127 |
patch_format = patch_format_dls; |
patch_format = patch_format_dls; |
128 |
break; |
break; |
129 |
case 3: |
case 4: // --gig |
130 |
patch_format = patch_format_gig; |
patch_format = patch_format_gig; |
131 |
break; |
break; |
132 |
case 4: |
case 5: // --instrument |
133 |
|
instrument_index = atoi(optarg); |
134 |
|
break; |
135 |
|
case 6: // --inputclient |
136 |
|
input_client = optarg; |
137 |
|
break; |
138 |
|
case 7: // --alsaout |
139 |
|
alsaout = optarg; |
140 |
|
use_jack = false; // If this option is specified do not connect to jack |
141 |
|
break; |
142 |
|
case 8: { // --jackout |
143 |
|
try { |
144 |
|
String arg(optarg); |
145 |
|
// remove outer apostrophes |
146 |
|
arg = arg.substr(arg.find('\'') + 1, arg.rfind('\'') - (arg.find('\'') + 1)); |
147 |
|
// split in two arguments |
148 |
|
jack_playback[0] = arg.substr(0, arg.find("\' ")); |
149 |
|
jack_playback[1] = arg.substr(arg.find("\' ") + 2, arg.size() - (arg.find("\' ") + 2)); |
150 |
|
// remove inner apostrophes |
151 |
|
jack_playback[0] = jack_playback[0].substr(0, jack_playback[0].find('\'')); |
152 |
|
jack_playback[1] = jack_playback[1].substr(jack_playback[1].find('\'') + 1, jack_playback[1].size() - jack_playback[1].find('\'')); |
153 |
|
// this is the default but set it up anyway in case alsa_card was also used. |
154 |
|
use_jack = true; |
155 |
|
} |
156 |
|
catch (...) { |
157 |
|
fprintf(stderr, "Invalid argument '%s' for parameter --jackout\n", optarg); |
158 |
|
exit(EXIT_FAILURE); |
159 |
|
} |
160 |
|
break; |
161 |
|
} |
162 |
|
case 9: // --samplerate |
163 |
|
samplerate = atoi(optarg); |
164 |
|
break; |
165 |
|
case 10: // --server |
166 |
|
run_server = true; |
167 |
|
break; |
168 |
|
case 11: // --help |
169 |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
170 |
|
printf("--gig loads a Gigasampler instrument\n"); |
171 |
|
printf("--dls loads a DLS instrument\n"); |
172 |
|
printf("--instrument index of the instrument in the instrument file if it\n"); |
173 |
|
printf(" contains more than one (default: 0)\n"); |
174 |
printf("--numfragments sets the number of audio fragments\n"); |
printf("--numfragments sets the number of audio fragments\n"); |
175 |
printf("--fragmentsize sets the fragment size\n"); |
printf("--fragmentsize sets the fragment size\n"); |
176 |
printf("--dls loads a DLS instrument\n"); |
printf("--volume sets global volume gain factor (a value > 1.0 means\n"); |
177 |
printf("--gig loads a Gigasampler instrument\n"); |
printf(" amplification, a value < 1.0 means attenuation,\n"); |
178 |
exit(0); |
printf(" default: 0.25)\n"); |
179 |
|
printf("--inputclient connects to an Alsa sequencer input client on startup\n"); |
180 |
|
printf(" (e.g. 64:0 to connect to a client with ID 64 and port 0)\n"); |
181 |
|
printf("--alsaout connects to the given Alsa sound device on startup\n"); |
182 |
|
printf(" (e.g. 0,0 to connect to hw:0,0 or plughw:0,0)\n"); |
183 |
|
printf("--jackout connects to the given Jack playback ports on startup\n"); |
184 |
|
printf(" (e.g. \"\'alsa_pcm:playback_1\' \'alsa_pcm:playback_2\'\"\n"); |
185 |
|
printf(" in case of stereo output)\n"); |
186 |
|
printf("--samplerate sets sample rate if supported by audio output system\n"); |
187 |
|
printf(" (e.g. 44100)\n"); |
188 |
|
printf("--server launch network server for remote control\n"); |
189 |
|
exit(EXIT_SUCCESS); |
190 |
break; |
break; |
191 |
} |
} |
192 |
} |
} |
193 |
} |
} |
194 |
} |
}*/ |