1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * |
6 |
* * |
7 |
* 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 * |
9 |
* the Free Software Foundation; either version 2 of the License, or * |
10 |
* (at your option) any later version. * |
11 |
* * |
12 |
* This program is distributed in the hope that it will be useful, * |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 |
* GNU General Public License for more details. * |
16 |
* * |
17 |
* You should have received a copy of the GNU General Public License * |
18 |
* along with this program; if not, write to the Free Software * |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
20 |
* MA 02111-1307 USA * |
21 |
***************************************************************************/ |
22 |
|
23 |
#include <stdio.h> |
24 |
#include <stdlib.h> |
25 |
#include <unistd.h> |
26 |
#include <getopt.h> |
27 |
#include <signal.h> |
28 |
#include <pthread.h> |
29 |
|
30 |
#include <string> |
31 |
|
32 |
#include "global.h" |
33 |
#include "audioio.h" |
34 |
#include "diskthread.h" |
35 |
#include "audiothread.h" |
36 |
#include "midiin.h" |
37 |
#include "stream.h" |
38 |
#include "RIFF.h" |
39 |
#include "gig.h" |
40 |
|
41 |
#define AUDIO_CHANNELS 2 // stereo |
42 |
#define AUDIO_FRAGMENTS 3 // 3 fragments, if it does not work set it to 2 |
43 |
#define AUDIO_FRAGMENTSIZE 512 // each fragment has 512 frames |
44 |
#define AUDIO_SAMPLERATE 44100 // Hz |
45 |
|
46 |
enum patch_format_t { |
47 |
patch_format_unknown, |
48 |
patch_format_gig, |
49 |
patch_format_dls |
50 |
} patch_format; |
51 |
|
52 |
AudioIO* pAudioIO; |
53 |
DiskThread* pDiskThread; |
54 |
AudioThread* pAudioThread; |
55 |
MidiIn* pMidiInThread; |
56 |
RIFF::File* pRIFF; |
57 |
gig::File* pGig; |
58 |
gig::Instrument* pInstrument; |
59 |
uint instrument_index; |
60 |
double volume; |
61 |
int num_fragments; |
62 |
int fragmentsize; |
63 |
std::string input_client; |
64 |
pthread_t signalhandlerthread; |
65 |
|
66 |
void parse_options(int argc, char **argv); |
67 |
void signal_handler(int signal); |
68 |
|
69 |
int main(int argc, char **argv) { |
70 |
pAudioIO = NULL; |
71 |
pRIFF = NULL; |
72 |
pGig = NULL; |
73 |
|
74 |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
75 |
signalhandlerthread = pthread_self(); |
76 |
signal(SIGINT, signal_handler); |
77 |
|
78 |
patch_format = patch_format_unknown; |
79 |
instrument_index = 0; |
80 |
num_fragments = AUDIO_FRAGMENTS; |
81 |
fragmentsize = AUDIO_FRAGMENTSIZE; |
82 |
volume = 0.25; // default volume |
83 |
|
84 |
// parse and assign command line options |
85 |
parse_options(argc, argv); |
86 |
|
87 |
if (patch_format != patch_format_gig) { |
88 |
printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n"); |
89 |
printf("Use 'linuxsampler --help' to see all available options.\n"); |
90 |
return EXIT_FAILURE; |
91 |
} |
92 |
|
93 |
dmsg(1,("Initializing audio output...")); |
94 |
pAudioIO = new AudioIO(); |
95 |
int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize); |
96 |
if (error) return EXIT_FAILURE; |
97 |
dmsg(1,("OK\n")); |
98 |
|
99 |
// Loading gig file |
100 |
try { |
101 |
printf("Loading gig file..."); |
102 |
fflush(stdout); |
103 |
pRIFF = new RIFF::File(argv[argc - 1]); |
104 |
pGig = new gig::File(pRIFF); |
105 |
pInstrument = pGig->GetInstrument(instrument_index); |
106 |
if (!pInstrument) { |
107 |
printf("there's no instrument with index %d.\n", instrument_index); |
108 |
exit(EXIT_FAILURE); |
109 |
} |
110 |
pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part |
111 |
printf("OK\n"); |
112 |
fflush(stdout); |
113 |
} |
114 |
catch (RIFF::Exception e) { |
115 |
e.PrintMessage(); |
116 |
return EXIT_FAILURE; |
117 |
} |
118 |
catch (...) { |
119 |
printf("Unknown exception while trying to parse gig file.\n"); |
120 |
return EXIT_FAILURE; |
121 |
} |
122 |
|
123 |
DiskThread* pDiskThread = new DiskThread(((pAudioIO->FragmentSize << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo |
124 |
AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); |
125 |
MidiIn* pMidiInThread = new MidiIn(pAudioThread); |
126 |
|
127 |
dmsg(1,("Starting disk thread...")); |
128 |
pDiskThread->StartThread(); |
129 |
dmsg(1,("OK\n")); |
130 |
dmsg(1,("Starting MIDI in thread...")); |
131 |
if (input_client.size() > 0) pMidiInThread->SubscribeToClient(input_client.c_str()); |
132 |
pMidiInThread->StartThread(); |
133 |
dmsg(1,("OK\n")); |
134 |
|
135 |
sleep(1); |
136 |
dmsg(1,("Starting audio thread...")); |
137 |
pAudioThread->Volume = volume; |
138 |
pAudioThread->StartThread(); |
139 |
dmsg(1,("OK\n")); |
140 |
|
141 |
printf("LinuxSampler initialization completed.\n"); |
142 |
|
143 |
while(true) { |
144 |
printf("Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d, Unused: %3.3d)\r", |
145 |
pAudioThread->ActiveVoiceCount, pAudioThread->ActiveVoiceCountMax, |
146 |
pDiskThread->ActiveStreamCount, pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); |
147 |
fflush(stdout); |
148 |
usleep(500000); |
149 |
} |
150 |
|
151 |
return EXIT_SUCCESS; |
152 |
} |
153 |
|
154 |
void signal_handler(int signal) { |
155 |
if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) { |
156 |
// stop all threads |
157 |
if (pMidiInThread) pMidiInThread->StopThread(); |
158 |
if (pAudioThread) pAudioThread->StopThread(); |
159 |
if (pDiskThread) pDiskThread->StopThread(); |
160 |
|
161 |
// free all resources |
162 |
if (pMidiInThread) delete pMidiInThread; |
163 |
if (pAudioThread) delete pAudioThread; |
164 |
if (pDiskThread) delete pDiskThread; |
165 |
if (pGig) delete pGig; |
166 |
if (pRIFF) delete pRIFF; |
167 |
if (pAudioIO) delete pAudioIO; |
168 |
|
169 |
printf("LinuxSampler stopped due to SIGINT\n"); |
170 |
exit(EXIT_SUCCESS); |
171 |
} |
172 |
} |
173 |
|
174 |
void parse_options(int argc, char **argv) { |
175 |
int res; |
176 |
int option_index = 0; |
177 |
static struct option long_options[] = |
178 |
{ |
179 |
{"numfragments",1,0,0}, |
180 |
{"fragmentsize",1,0,0}, |
181 |
{"volume",1,0,0}, |
182 |
{"dls",0,0,0}, |
183 |
{"gig",0,0,0}, |
184 |
{"instrument",1,0,0}, |
185 |
{"inputclient",1,0,0}, |
186 |
{"help",0,0,0}, |
187 |
{0,0,0,0} |
188 |
}; |
189 |
|
190 |
while (true) { |
191 |
res = getopt_long_only(argc, argv, "", long_options, &option_index); |
192 |
if(res == -1) break; |
193 |
if (res == 0) { |
194 |
switch(option_index) { |
195 |
case 0: |
196 |
num_fragments = atoi(optarg); |
197 |
break; |
198 |
case 1: |
199 |
fragmentsize = atoi(optarg); |
200 |
break; |
201 |
case 2: |
202 |
volume = atof(optarg); |
203 |
break; |
204 |
case 3: |
205 |
patch_format = patch_format_dls; |
206 |
break; |
207 |
case 4: |
208 |
patch_format = patch_format_gig; |
209 |
break; |
210 |
case 5: |
211 |
instrument_index = atoi(optarg); |
212 |
break; |
213 |
case 6: |
214 |
input_client = optarg; |
215 |
break; |
216 |
case 7: |
217 |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
218 |
printf("--gig loads a Gigasampler instrument\n"); |
219 |
printf("--dls loads a DLS instrument\n"); |
220 |
printf("--instrument index of the instrument in the instrument file if it\n"); |
221 |
printf(" contains more than one (default: 0)\n"); |
222 |
printf("--numfragments sets the number of audio fragments\n"); |
223 |
printf("--fragmentsize sets the fragment size\n"); |
224 |
printf("--volume sets global volume gain factor (a value > 1.0 means\n"); |
225 |
printf(" amplification, a value < 1.0 means attenuation,\n"); |
226 |
printf(" default: 0.25)\n"); |
227 |
printf("--inputclient connects to an Alsa sequencer input client on startup\n"); |
228 |
printf(" (e.g. 64:0 to connect to a client with ID 64 and port 0)\n"); |
229 |
exit(0); |
230 |
break; |
231 |
} |
232 |
} |
233 |
} |
234 |
} |