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 "diskthread.h" |
#include "drivers/midi/MidiInputDeviceFactory.h" |
28 |
#include "audiothread.h" |
#include "drivers/audio/AudioOutputDeviceFactory.h" |
29 |
#include "alsaio.h" |
#include "network/lscpserver.h" |
30 |
#include "jackio.h" |
#include "common/stacktrace.h" |
31 |
#include "midiin.h" |
#include "common/Features.h" |
32 |
#include "stream.h" |
|
33 |
#include "RIFF.h" |
using namespace LinuxSampler; |
34 |
#include "gig.h" |
|
35 |
|
Sampler* pSampler = NULL; |
36 |
#define AUDIO_CHANNELS 2 // stereo |
LSCPServer* pLSCPServer = NULL; |
37 |
#define AUDIO_FRAGMENTS 3 // 3 fragments, if it does not work set it to 2 |
pthread_t main_thread; |
|
#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; |
|
|
uint instrument_index; |
|
|
double volume; |
|
|
int num_fragments; |
|
|
int fragmentsize; |
|
|
String input_client; |
|
|
String alsaout; |
|
|
String jack_playback[2]; |
|
|
bool use_jack; |
|
|
pthread_t signalhandlerthread; |
|
|
uint samplerate; |
|
38 |
|
|
39 |
void parse_options(int argc, char **argv); |
void parse_options(int argc, char **argv); |
40 |
void signal_handler(int signal); |
void signal_handler(int signal); |
41 |
|
void kill_app(); |
42 |
|
|
43 |
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
44 |
pAudioIO = NULL; |
|
45 |
pRIFF = NULL; |
// initialize the stack trace mechanism with our binary file |
46 |
pGig = NULL; |
StackTraceInit(argv[0], -1); |
47 |
|
|
48 |
|
main_thread = pthread_self(); |
49 |
|
|
50 |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
|
signalhandlerthread = pthread_self(); |
|
51 |
signal(SIGINT, signal_handler); |
signal(SIGINT, signal_handler); |
52 |
|
|
53 |
patch_format = patch_format_unknown; |
// register signal handler for all unusual signals |
54 |
instrument_index = 0; |
// (we will print the stack trace and exit) |
55 |
num_fragments = AUDIO_FRAGMENTS; |
struct sigaction sact; |
56 |
fragmentsize = AUDIO_FRAGMENTSIZE; |
sigemptyset(&sact.sa_mask); |
57 |
volume = 0.25; // default volume |
sact.sa_flags = 0; |
58 |
alsaout = "0,0"; // default card |
sact.sa_handler = signal_handler; |
59 |
jack_playback[0] = ""; |
sigaction(SIGSEGV, &sact, NULL); |
60 |
jack_playback[1] = ""; |
sigaction(SIGBUS, &sact, NULL); |
61 |
samplerate = AUDIO_SAMPLERATE; |
sigaction(SIGILL, &sact, NULL); |
62 |
use_jack = true; |
sigaction(SIGFPE, &sact, NULL); |
63 |
|
sigaction(SIGUSR1, &sact, NULL); |
64 |
|
sigaction(SIGUSR2, &sact, NULL); |
65 |
|
|
66 |
// parse and assign command line options |
// parse and assign command line options |
67 |
parse_options(argc, argv); |
//parse_options(argc, argv); |
68 |
|
|
69 |
if (patch_format != patch_format_gig) { |
dmsg(1,("LinuxSampler %s\n", VERSION)); |
70 |
printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n"); |
dmsg(1,("Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck\n")); |
|
printf("Use 'linuxsampler --help' to see all available options.\n"); |
|
|
return EXIT_FAILURE; |
|
|
} |
|
71 |
|
|
72 |
int error = 1; |
// detect and print system / CPU specific features |
73 |
#if HAVE_JACK |
String sFeatures; |
74 |
if (use_jack) { |
Features::detect(); |
75 |
dmsg(1,("Initializing audio output (Jack)...")); |
#if ARCH_X86 |
76 |
pAudioIO = new JackIO(); |
if (Features::supportsMMX()) sFeatures += " MMX"; |
77 |
error = ((JackIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, jack_playback); |
if (Features::supportsSSE()) sFeatures += " SSE"; |
78 |
if (error) dmsg(1,("Trying Alsa output instead.\n")); |
#endif // ARCH_X86 |
79 |
} |
if (!sFeatures.size()) sFeatures = " None"; |
80 |
#endif // HAVE_JACK |
dmsg(1,("Detected features:%s\n",sFeatures.c_str())); |
81 |
if (error) { |
|
82 |
dmsg(1,("Initializing audio output (Alsa)...")); |
// create LinuxSampler instance |
83 |
pAudioIO = new AlsaIO(); |
dmsg(1,("Creating Sampler...")); |
84 |
int error = ((AlsaIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, samplerate, num_fragments, fragmentsize, alsaout); |
pSampler = new Sampler; |
|
if (error) return EXIT_FAILURE; |
|
|
} |
|
85 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
86 |
|
|
87 |
// Loading gig file |
dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str())); |
88 |
try { |
dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str())); |
|
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; |
|
|
} |
|
|
|
|
|
DiskThread* pDiskThread = new DiskThread(((pAudioIO->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo |
|
|
AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); |
|
|
MidiIn* pMidiInThread = new MidiIn(pAudioThread); |
|
|
|
|
|
dmsg(1,("Starting disk thread...")); |
|
|
pDiskThread->StartThread(); |
|
|
dmsg(1,("OK\n")); |
|
|
dmsg(1,("Starting MIDI in thread...")); |
|
|
if (input_client.size() > 0) pMidiInThread->SubscribeToClient(input_client.c_str()); |
|
|
pMidiInThread->StartThread(); |
|
|
dmsg(1,("OK\n")); |
|
89 |
|
|
90 |
sleep(1); |
// start LSCP network server |
91 |
dmsg(1,("Starting audio thread...")); |
dmsg(1,("Starting LSCP network server (on TCP port %d)...", LSCP_PORT)); |
92 |
pAudioThread->Volume = volume; |
pLSCPServer = new LSCPServer(pSampler); |
93 |
pAudioIO->AssignEngine(pAudioThread); |
pLSCPServer->StartThread(); |
94 |
pAudioIO->Activate(); |
pLSCPServer->WaitUntilInitialized(); |
95 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
96 |
|
|
97 |
printf("LinuxSampler initialization completed.\n"); |
printf("LinuxSampler initialization completed.\n"); |
98 |
|
|
99 |
while(true) { |
while(true) { |
100 |
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", |
101 |
pAudioThread->ActiveVoiceCount, pAudioThread->ActiveVoiceCountMax, |
pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax, |
102 |
pDiskThread->ActiveStreamCount, pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); |
pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); |
103 |
fflush(stdout); |
fflush(stdout);*/ |
104 |
usleep(500000); |
usleep(500000); |
105 |
} |
} |
106 |
|
|
107 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
108 |
} |
} |
109 |
|
|
110 |
void signal_handler(int signal) { |
void signal_handler(int iSignal) { |
111 |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
switch (iSignal) { |
112 |
// stop all threads |
case SIGINT: { |
113 |
if (pAudioIO) pAudioIO->Close(); |
if (pthread_equal(pthread_self(), main_thread)) { |
114 |
if (pMidiInThread) pMidiInThread->StopThread(); |
if (pLSCPServer) { |
115 |
if (pDiskThread) pDiskThread->StopThread(); |
pLSCPServer->StopThread(); |
116 |
|
delete pLSCPServer; |
117 |
// free all resources |
} |
118 |
if (pMidiInThread) delete pMidiInThread; |
if (pSampler) delete pSampler; |
119 |
if (pAudioThread) delete pAudioThread; |
printf("LinuxSampler stopped due to SIGINT.\n"); |
120 |
if (pDiskThread) delete pDiskThread; |
exit(EXIT_SUCCESS); |
121 |
if (pGig) delete pGig; |
} |
122 |
if (pRIFF) delete pRIFF; |
return; |
123 |
if (pAudioIO) delete pAudioIO; |
} |
124 |
|
case SIGSEGV: |
125 |
printf("LinuxSampler stopped due to SIGINT\n"); |
std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush; |
126 |
exit(EXIT_SUCCESS); |
break; |
127 |
|
case SIGBUS: |
128 |
|
std::cerr << ">>> FATAL ERROR: Access to undefined portion of a memory object (SIGBUS) occured! <<<\n" << std::flush; |
129 |
|
break; |
130 |
|
case SIGILL: |
131 |
|
std::cerr << ">>> FATAL ERROR: Illegal instruction (SIGILL) occured! <<<\n" << std::flush; |
132 |
|
break; |
133 |
|
case SIGFPE: |
134 |
|
std::cerr << ">>> FATAL ERROR: Erroneous arithmetic operation (SIGFPE) occured! <<<\n" << std::flush; |
135 |
|
break; |
136 |
|
case SIGUSR1: |
137 |
|
std::cerr << ">>> User defined signal 1 (SIGUSR1) received <<<\n" << std::flush; |
138 |
|
break; |
139 |
|
case SIGUSR2: |
140 |
|
std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush; |
141 |
|
break; |
142 |
|
default: { // this should never happen, as we register for the signals we want |
143 |
|
std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush; |
144 |
|
break; |
145 |
|
} |
146 |
} |
} |
147 |
|
signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions |
148 |
|
std::cerr << "Showing stack trace...\n" << std::flush; |
149 |
|
StackTrace(); |
150 |
|
sleep(2); |
151 |
|
std::cerr << "Killing LinuxSampler...\n" << std::flush; |
152 |
|
kill_app(); // Use abort() if we want to generate a core dump. |
153 |
|
} |
154 |
|
|
155 |
|
void kill_app() { |
156 |
|
kill(main_thread, SIGKILL); |
157 |
} |
} |
158 |
|
|
159 |
void parse_options(int argc, char **argv) { |
/*void parse_options(int argc, char **argv) { |
160 |
int res; |
int res; |
161 |
int option_index = 0; |
int option_index = 0; |
162 |
static struct option long_options[] = |
static struct option long_options[] = |
171 |
{"alsaout",1,0,0}, |
{"alsaout",1,0,0}, |
172 |
{"jackout",1,0,0}, |
{"jackout",1,0,0}, |
173 |
{"samplerate",1,0,0}, |
{"samplerate",1,0,0}, |
174 |
|
{"server",0,0,0}, |
175 |
{"help",0,0,0}, |
{"help",0,0,0}, |
176 |
{0,0,0,0} |
{0,0,0,0} |
177 |
}; |
}; |
229 |
case 9: // --samplerate |
case 9: // --samplerate |
230 |
samplerate = atoi(optarg); |
samplerate = atoi(optarg); |
231 |
break; |
break; |
232 |
case 10: // --help |
case 10: // --server |
233 |
|
run_server = true; |
234 |
|
break; |
235 |
|
case 11: // --help |
236 |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
237 |
printf("--gig loads a Gigasampler instrument\n"); |
printf("--gig loads a Gigasampler instrument\n"); |
238 |
printf("--dls loads a DLS instrument\n"); |
printf("--dls loads a DLS instrument\n"); |
252 |
printf(" in case of stereo output)\n"); |
printf(" in case of stereo output)\n"); |
253 |
printf("--samplerate sets sample rate if supported by audio output system\n"); |
printf("--samplerate sets sample rate if supported by audio output system\n"); |
254 |
printf(" (e.g. 44100)\n"); |
printf(" (e.g. 44100)\n"); |
255 |
|
printf("--server launch network server for remote control\n"); |
256 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
257 |
break; |
break; |
258 |
} |
} |
259 |
} |
} |
260 |
} |
} |
261 |
} |
}*/ |