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 "network/lscpserver.h" |
|
#include "audiothread.h" |
|
|
#include "alsaio.h" |
|
|
#include "jackio.h" |
|
|
#include "midiin.h" |
|
|
#include "stream.h" |
|
|
#include "RIFF.h" |
|
|
#include "gig.h" |
|
28 |
|
|
29 |
|
#if 0 |
30 |
#define AUDIO_CHANNELS 2 // stereo |
#define AUDIO_CHANNELS 2 // stereo |
31 |
#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 |
32 |
#define AUDIO_FRAGMENTSIZE 512 // each fragment has 512 frames |
#define AUDIO_FRAGMENTSIZE 512 // each fragment has 512 frames |
33 |
#define AUDIO_SAMPLERATE 44100 // Hz |
#define AUDIO_SAMPLERATE 44100 // Hz |
34 |
|
#endif |
35 |
|
|
36 |
enum patch_format_t { |
using namespace LinuxSampler; |
37 |
|
|
38 |
|
/*enum patch_format_t { |
39 |
patch_format_unknown, |
patch_format_unknown, |
40 |
patch_format_gig, |
patch_format_gig, |
41 |
patch_format_dls |
patch_format_dls |
42 |
} patch_format; |
} patch_format = patch_format_unknown;*/ |
43 |
|
|
44 |
|
Sampler* pSampler = NULL; |
45 |
|
LSCPServer* pLSCPServer = NULL; |
46 |
|
pthread_t signalhandlerthread; |
47 |
|
/*AudioThread* pEngine = NULL; |
48 |
|
uint instrument_index = 0; |
49 |
|
double volume = 0.25; |
50 |
|
int num_fragments = AUDIO_FRAGMENTS; |
51 |
|
int fragmentsize = AUDIO_FRAGMENTSIZE; |
52 |
|
uint samplerate = AUDIO_SAMPLERATE; |
53 |
|
String input_client; |
54 |
|
String alsaout = "0,0"; // default card |
55 |
|
String jack_playback[2] = { "", "" }; |
56 |
|
bool use_jack = true; |
57 |
|
bool run_server = false;*/ |
58 |
|
|
|
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; |
|
59 |
|
|
60 |
void parse_options(int argc, char **argv); |
void parse_options(int argc, char **argv); |
61 |
void signal_handler(int signal); |
void signal_handler(int signal); |
62 |
|
|
63 |
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
|
pAudioIO = NULL; |
|
|
pRIFF = NULL; |
|
|
pGig = NULL; |
|
64 |
|
|
65 |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
66 |
signalhandlerthread = pthread_self(); |
signalhandlerthread = pthread_self(); |
67 |
signal(SIGINT, signal_handler); |
signal(SIGINT, signal_handler); |
68 |
|
|
|
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; |
|
|
|
|
69 |
// parse and assign command line options |
// parse and assign command line options |
70 |
parse_options(argc, argv); |
//parse_options(argc, argv); |
71 |
|
|
72 |
if (patch_format != patch_format_gig) { |
/*if (patch_format != patch_format_gig) { |
73 |
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"); |
74 |
printf("Use 'linuxsampler --help' to see all available options.\n"); |
printf("Use 'linuxsampler --help' to see all available options.\n"); |
75 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
76 |
} |
}*/ |
77 |
|
|
78 |
|
// create LinuxSampler instance |
79 |
|
pSampler = new Sampler; |
80 |
|
|
81 |
int error = 1; |
// create an audio output device |
82 |
|
/* bool no_jack = true; |
83 |
#if HAVE_JACK |
#if HAVE_JACK |
84 |
if (use_jack) { |
if (use_jack) { |
85 |
dmsg(1,("Initializing audio output (Jack)...")); |
dmsg(1,("Creating audio output device (Jack)...")); |
86 |
pAudioIO = new JackIO(); |
try { |
87 |
error = ((JackIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, jack_playback); |
pSampler->CreateAudioOutputDevice(audio_output_type_jack); |
88 |
if (error) dmsg(1,("Trying Alsa output instead.\n")); |
no_jack = false; |
89 |
|
} |
90 |
|
catch (AudioOutputException aoe) { |
91 |
|
aoe.PrintMessage(); |
92 |
|
dmsg(1,("Trying to create Alsa output device instead.\n")); |
93 |
|
} |
94 |
} |
} |
95 |
#endif // HAVE_JACK |
#endif // HAVE_JACK |
96 |
if (error) { |
if (no_jack) { |
97 |
dmsg(1,("Initializing audio output (Alsa)...")); |
dmsg(1,("Creating audio output device (Alsa)...")); |
98 |
pAudioIO = new AlsaIO(); |
try { |
99 |
int error = ((AlsaIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, samplerate, num_fragments, fragmentsize, alsaout); |
pSampler->CreateAudioOutputDevice(audio_output_type_alsa); |
100 |
if (error) return EXIT_FAILURE; |
} |
101 |
} |
catch (AudioOutputException aoe) { |
102 |
dmsg(1,("OK\n")); |
aoe.PrintMessage(); |
103 |
|
dmsg(1,("Trying to create Alsa output device instead.\n")); |
104 |
// Loading gig file |
return EXIT_FAILURE; |
|
try { |
|
|
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); |
|
105 |
} |
} |
|
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; |
|
106 |
} |
} |
107 |
|
dmsg(1,("OK\n"));*/ |
108 |
|
|
109 |
DiskThread* pDiskThread = new DiskThread(((pAudioIO->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo |
// start LSCP network server |
110 |
AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); |
dmsg(1,("Starting network server...")); |
111 |
MidiIn* pMidiInThread = new MidiIn(pAudioThread); |
pLSCPServer = new LSCPServer(pSampler); |
112 |
|
pLSCPServer->StartThread(); |
|
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")); |
|
|
|
|
|
sleep(1); |
|
|
dmsg(1,("Starting audio thread...")); |
|
|
pAudioThread->Volume = volume; |
|
|
pAudioIO->AssignEngine(pAudioThread); |
|
|
pAudioIO->Activate(); |
|
113 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
114 |
|
|
115 |
printf("LinuxSampler initialization completed.\n"); |
printf("LinuxSampler initialization completed.\n"); |
116 |
|
|
117 |
while(true) { |
while(true) { |
118 |
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", |
119 |
pAudioThread->ActiveVoiceCount, pAudioThread->ActiveVoiceCountMax, |
pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax, |
120 |
pDiskThread->ActiveStreamCount, pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); |
pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); |
121 |
fflush(stdout); |
fflush(stdout);*/ |
122 |
usleep(500000); |
usleep(500000); |
123 |
} |
} |
124 |
|
|
127 |
|
|
128 |
void signal_handler(int signal) { |
void signal_handler(int signal) { |
129 |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
130 |
// stop all threads |
if (pLSCPServer) { |
131 |
if (pAudioIO) pAudioIO->Close(); |
pLSCPServer->StopThread(); |
132 |
if (pMidiInThread) pMidiInThread->StopThread(); |
delete pLSCPServer; |
133 |
if (pDiskThread) pDiskThread->StopThread(); |
} |
134 |
|
if (pSampler) delete pSampler; |
|
// 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; |
|
|
|
|
135 |
printf("LinuxSampler stopped due to SIGINT\n"); |
printf("LinuxSampler stopped due to SIGINT\n"); |
136 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
137 |
} |
} |
138 |
} |
} |
139 |
|
|
140 |
void parse_options(int argc, char **argv) { |
/*void parse_options(int argc, char **argv) { |
141 |
int res; |
int res; |
142 |
int option_index = 0; |
int option_index = 0; |
143 |
static struct option long_options[] = |
static struct option long_options[] = |
152 |
{"alsaout",1,0,0}, |
{"alsaout",1,0,0}, |
153 |
{"jackout",1,0,0}, |
{"jackout",1,0,0}, |
154 |
{"samplerate",1,0,0}, |
{"samplerate",1,0,0}, |
155 |
|
{"server",0,0,0}, |
156 |
{"help",0,0,0}, |
{"help",0,0,0}, |
157 |
{0,0,0,0} |
{0,0,0,0} |
158 |
}; |
}; |
210 |
case 9: // --samplerate |
case 9: // --samplerate |
211 |
samplerate = atoi(optarg); |
samplerate = atoi(optarg); |
212 |
break; |
break; |
213 |
case 10: // --help |
case 10: // --server |
214 |
|
run_server = true; |
215 |
|
break; |
216 |
|
case 11: // --help |
217 |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
218 |
printf("--gig loads a Gigasampler instrument\n"); |
printf("--gig loads a Gigasampler instrument\n"); |
219 |
printf("--dls loads a DLS instrument\n"); |
printf("--dls loads a DLS instrument\n"); |
233 |
printf(" in case of stereo output)\n"); |
printf(" in case of stereo output)\n"); |
234 |
printf("--samplerate sets sample rate if supported by audio output system\n"); |
printf("--samplerate sets sample rate if supported by audio output system\n"); |
235 |
printf(" (e.g. 44100)\n"); |
printf(" (e.g. 44100)\n"); |
236 |
|
printf("--server launch network server for remote control\n"); |
237 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
238 |
break; |
break; |
239 |
} |
} |
240 |
} |
} |
241 |
} |
} |
242 |
} |
}*/ |