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" |
|
#include "alsaio.h" |
|
|
#include "jackio.h" |
|
|
#include "midiin.h" |
|
|
#include "stream.h" |
|
|
#include "RIFF.h" |
|
|
#include "gig.h" |
|
29 |
#include "network/lscpserver.h" |
#include "network/lscpserver.h" |
30 |
|
#include "common/stacktrace.h" |
31 |
|
|
32 |
#define AUDIO_CHANNELS 2 // stereo |
using namespace LinuxSampler; |
33 |
#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 |
Sampler* pSampler = NULL; |
35 |
#define AUDIO_SAMPLERATE 44100 // Hz |
LSCPServer* pLSCPServer = NULL; |
36 |
|
pthread_t main_thread; |
|
enum patch_format_t { |
|
|
patch_format_unknown, |
|
|
patch_format_gig, |
|
|
patch_format_dls |
|
|
} patch_format = patch_format_unknown; |
|
|
|
|
|
AudioIO* pAudioIO = NULL; |
|
|
MidiIn* pMidiInThread = NULL; |
|
|
LSCPServer* pLSCPServer = NULL; |
|
|
AudioThread* pEngine = NULL; |
|
|
uint instrument_index = 0; |
|
|
double volume = 0.25; |
|
|
int num_fragments = AUDIO_FRAGMENTS; |
|
|
int fragmentsize = AUDIO_FRAGMENTSIZE; |
|
|
uint samplerate = AUDIO_SAMPLERATE; |
|
|
String input_client; |
|
|
String alsaout = "0,0"; // default card |
|
|
String jack_playback[2] = { "", "" }; |
|
|
bool use_jack = true; |
|
|
bool run_server = false; |
|
|
pthread_t signalhandlerthread; |
|
37 |
|
|
38 |
void parse_options(int argc, char **argv); |
void parse_options(int argc, char **argv); |
39 |
void signal_handler(int signal); |
void signal_handler(int signal); |
40 |
|
void kill_app(); |
41 |
|
|
42 |
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
43 |
|
|
44 |
|
// initialize the stack trace mechanism with our binary file |
45 |
|
StackTraceInit(argv[0], -1); |
46 |
|
|
47 |
|
main_thread = pthread_self(); |
48 |
|
|
49 |
// 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(); |
|
50 |
signal(SIGINT, signal_handler); |
signal(SIGINT, signal_handler); |
51 |
|
|
52 |
// parse and assign command line options |
// register signal handler for all unusual signals |
53 |
parse_options(argc, argv); |
// (we will print the stack trace and exit) |
54 |
|
struct sigaction sact; |
55 |
if (patch_format != patch_format_gig) { |
sigemptyset(&sact.sa_mask); |
56 |
printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n"); |
sact.sa_flags = 0; |
57 |
printf("Use 'linuxsampler --help' to see all available options.\n"); |
sact.sa_handler = signal_handler; |
58 |
return EXIT_FAILURE; |
sigaction(SIGSEGV, &sact, NULL); |
59 |
} |
sigaction(SIGBUS, &sact, NULL); |
60 |
|
sigaction(SIGILL, &sact, NULL); |
61 |
|
sigaction(SIGFPE, &sact, NULL); |
62 |
|
sigaction(SIGUSR1, &sact, NULL); |
63 |
|
sigaction(SIGUSR2, &sact, NULL); |
64 |
|
|
65 |
int error = 1; |
// parse and assign command line options |
66 |
#if HAVE_JACK |
//parse_options(argc, argv); |
|
if (use_jack) { |
|
|
dmsg(1,("Initializing audio output (Jack)...")); |
|
|
pAudioIO = new JackIO(); |
|
|
error = ((JackIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, jack_playback); |
|
|
if (error) dmsg(1,("Trying Alsa output instead.\n")); |
|
|
} |
|
|
#endif // HAVE_JACK |
|
|
if (error) { |
|
|
dmsg(1,("Initializing audio output (Alsa)...")); |
|
|
pAudioIO = new AlsaIO(); |
|
|
int error = ((AlsaIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, samplerate, num_fragments, fragmentsize, alsaout); |
|
|
if (error) return EXIT_FAILURE; |
|
|
} |
|
|
dmsg(1,("OK\n")); |
|
67 |
|
|
68 |
AudioThread* pEngine = new AudioThread(pAudioIO); |
dmsg(1,("LinuxSampler %s\n", VERSION)); |
69 |
MidiIn* pMidiInThread = new MidiIn(pEngine); |
dmsg(1,("Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck\n")); |
70 |
|
|
71 |
// Loading gig file |
// create LinuxSampler instance |
72 |
result_t result = pEngine->LoadInstrument(argv[argc - 1], instrument_index); |
dmsg(1,("Creating Sampler...")); |
73 |
if (result.type == result_type_error) return EXIT_FAILURE; |
pSampler = new Sampler; |
|
pEngine->Volume = volume; |
|
|
|
|
|
dmsg(1,("Starting MIDI in thread...")); |
|
|
if (input_client.size() > 0) pMidiInThread->SubscribeToClient(input_client.c_str()); |
|
|
pMidiInThread->StartThread(); |
|
74 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
75 |
|
|
76 |
sleep(1); |
dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str())); |
77 |
|
dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str())); |
78 |
|
|
79 |
dmsg(1,("Starting audio thread...")); |
// start LSCP network server |
80 |
pAudioIO->AssignEngine(pEngine); |
dmsg(1,("Starting LSCP network server (on TCP port %d)...", LSCP_PORT)); |
81 |
pAudioIO->Activate(); |
pLSCPServer = new LSCPServer(pSampler); |
82 |
|
pLSCPServer->StartThread(); |
83 |
|
pLSCPServer->WaitUntilInitialized(); |
84 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
85 |
|
|
|
if (run_server) { |
|
|
dmsg(1,("Starting network server...")); |
|
|
pLSCPServer = new LSCPServer(pEngine); |
|
|
pLSCPServer->StartThread(); |
|
|
dmsg(1,("OK\n")); |
|
|
} |
|
|
|
|
86 |
printf("LinuxSampler initialization completed.\n"); |
printf("LinuxSampler initialization completed.\n"); |
87 |
|
|
88 |
while(true) { |
while(true) { |
89 |
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", |
90 |
pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax, |
pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax, |
91 |
pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); |
pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); |
92 |
fflush(stdout); |
fflush(stdout);*/ |
93 |
usleep(500000); |
usleep(500000); |
94 |
} |
} |
95 |
|
|
96 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
97 |
} |
} |
98 |
|
|
99 |
void signal_handler(int signal) { |
void signal_handler(int iSignal) { |
100 |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
switch (iSignal) { |
101 |
// stop all threads |
case SIGINT: { |
102 |
if (pAudioIO) pAudioIO->Close(); |
if (pthread_equal(pthread_self(), main_thread)) { |
103 |
if (pMidiInThread) pMidiInThread->StopThread(); |
if (pLSCPServer) { |
104 |
|
pLSCPServer->StopThread(); |
105 |
// free all resources |
delete pLSCPServer; |
106 |
if (pMidiInThread) delete pMidiInThread; |
} |
107 |
if (pEngine) delete pEngine; |
if (pSampler) delete pSampler; |
108 |
if (pAudioIO) delete pAudioIO; |
printf("LinuxSampler stopped due to SIGINT.\n"); |
109 |
|
exit(EXIT_SUCCESS); |
110 |
printf("LinuxSampler stopped due to SIGINT\n"); |
} |
111 |
exit(EXIT_SUCCESS); |
return; |
112 |
|
} |
113 |
|
case SIGSEGV: |
114 |
|
std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush; |
115 |
|
break; |
116 |
|
case SIGBUS: |
117 |
|
std::cerr << ">>> FATAL ERROR: Access to undefined portion of a memory object (SIGBUS) occured! <<<\n" << std::flush; |
118 |
|
break; |
119 |
|
case SIGILL: |
120 |
|
std::cerr << ">>> FATAL ERROR: Illegal instruction (SIGILL) occured! <<<\n" << std::flush; |
121 |
|
break; |
122 |
|
case SIGFPE: |
123 |
|
std::cerr << ">>> FATAL ERROR: Erroneous arithmetic operation (SIGFPE) occured! <<<\n" << std::flush; |
124 |
|
break; |
125 |
|
case SIGUSR1: |
126 |
|
std::cerr << ">>> User defined signal 1 (SIGUSR1) received <<<\n" << std::flush; |
127 |
|
break; |
128 |
|
case SIGUSR2: |
129 |
|
std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush; |
130 |
|
break; |
131 |
|
default: { // this should never happen, as we register for the signals we want |
132 |
|
std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush; |
133 |
|
break; |
134 |
|
} |
135 |
} |
} |
136 |
|
signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions |
137 |
|
std::cerr << "Showing stack trace...\n" << std::flush; |
138 |
|
StackTrace(); |
139 |
|
sleep(2); |
140 |
|
std::cerr << "Killing LinuxSampler...\n" << std::flush; |
141 |
|
kill_app(); // Use abort() if we want to generate a core dump. |
142 |
} |
} |
143 |
|
|
144 |
void parse_options(int argc, char **argv) { |
void kill_app() { |
145 |
|
kill(main_thread, SIGKILL); |
146 |
|
} |
147 |
|
|
148 |
|
/*void parse_options(int argc, char **argv) { |
149 |
int res; |
int res; |
150 |
int option_index = 0; |
int option_index = 0; |
151 |
static struct option long_options[] = |
static struct option long_options[] = |
247 |
} |
} |
248 |
} |
} |
249 |
} |
} |
250 |
} |
}*/ |