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 |
|
29 |
#include "global.h" |
30 |
#include "audioio.h" |
31 |
#include "diskthread.h" |
32 |
#include "audiothread.h" |
33 |
#include "midiin.h" |
34 |
#include "stream.h" |
35 |
#include "RIFF.h" |
36 |
#include "gig.h" |
37 |
|
38 |
#define AUDIO_CHANNELS 2 // stereo |
39 |
#define AUDIO_FRAGMENTS 3 // 3 fragments, if it does not work set it to 2 |
40 |
#define AUDIO_FRAGMENTSIZE 512 // each fragment has 512 frames |
41 |
#define AUDIO_SAMPLERATE 44100 // Hz |
42 |
|
43 |
enum patch_format_t { |
44 |
patch_format_unknown, |
45 |
patch_format_gig, |
46 |
patch_format_dls |
47 |
} patch_format; |
48 |
|
49 |
AudioIO* pAudioIO; |
50 |
DiskThread* pDiskThread; |
51 |
AudioThread* pAudioThread; |
52 |
MidiIn* pMidiInThread; |
53 |
|
54 |
RIFF::File* pRIFF; |
55 |
gig::File* pGig; |
56 |
gig::Instrument* pInstrument; |
57 |
|
58 |
void parse_options(int argc, char **argv); |
59 |
void signal_handler(int signal); |
60 |
|
61 |
int midi_non_blocking; |
62 |
int num_fragments; |
63 |
int fragmentsize; |
64 |
bool instrument_is_DLS; |
65 |
bool instruemtn_is_gig; |
66 |
char midi_device[40]; |
67 |
|
68 |
int main(int argc, char **argv) { |
69 |
pAudioIO = NULL; |
70 |
pRIFF = NULL; |
71 |
pGig = NULL; |
72 |
|
73 |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
74 |
signal(SIGINT, signal_handler); |
75 |
|
76 |
patch_format = patch_format_unknown; |
77 |
midi_non_blocking = 1; |
78 |
num_fragments = AUDIO_FRAGMENTS; |
79 |
fragmentsize = AUDIO_FRAGMENTSIZE; |
80 |
strcpy(midi_device, "/dev/midi00"); |
81 |
|
82 |
// parse and assign command line options |
83 |
parse_options(argc, argv); |
84 |
|
85 |
if (patch_format != patch_format_gig) { |
86 |
printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig\n"); |
87 |
printf("to load a .gig file!\n"); |
88 |
return EXIT_FAILURE; |
89 |
} |
90 |
|
91 |
dmsg(("Initializing audio output...")); |
92 |
pAudioIO = new AudioIO(); |
93 |
int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize); |
94 |
if (error) return EXIT_FAILURE; |
95 |
dmsg(("OK\n")); |
96 |
|
97 |
// Loading gig file |
98 |
try { |
99 |
printf("Loading gig file..."); |
100 |
fflush(stdout); |
101 |
pRIFF = new RIFF::File(argv[argc - 1]); |
102 |
pGig = new gig::File(pRIFF); |
103 |
pInstrument = pGig->GetFirstInstrument(); |
104 |
pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part |
105 |
printf("OK\n"); |
106 |
fflush(stdout); |
107 |
} |
108 |
catch (RIFF::Exception e) { |
109 |
e.PrintMessage(); |
110 |
return EXIT_FAILURE; |
111 |
} |
112 |
catch (...) { |
113 |
printf("Unknown exception while trying to parse gig file.\n"); |
114 |
return EXIT_FAILURE; |
115 |
} |
116 |
|
117 |
DiskThread* pDiskThread = new DiskThread(((pAudioIO->FragmentSize << MAX_PITCH) << 1) + 3); //FIXME: assuming stereo |
118 |
AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); |
119 |
MidiIn* pMidiInThread = new MidiIn(pAudioThread); |
120 |
|
121 |
dmsg(("Starting disk thread...")); |
122 |
pDiskThread->StartThread(); |
123 |
dmsg(("OK\n")); |
124 |
dmsg(("Starting MIDI in thread...")); |
125 |
pMidiInThread->StartThread(); |
126 |
dmsg(("OK\n")); |
127 |
|
128 |
sleep(1); |
129 |
dmsg(("Starting audio thread...")); |
130 |
pAudioThread->StartThread(); |
131 |
dmsg(("OK\n")); |
132 |
|
133 |
printf("LinuxSampler initialization completed.\n"); |
134 |
|
135 |
while(true) { |
136 |
printf("Voices: %3.3d Streams: %3.3d \r",pAudioThread->ActiveVoiceCount, pDiskThread->ActiveStreamCount); fflush(stdout); |
137 |
usleep(500000); |
138 |
} |
139 |
|
140 |
return EXIT_SUCCESS; |
141 |
} |
142 |
|
143 |
void signal_handler(int signal) { |
144 |
if (signal == SIGINT) { |
145 |
// stop all threads |
146 |
if (pMidiInThread) pMidiInThread->StopThread(); |
147 |
if (pAudioThread) pAudioThread->StopThread(); |
148 |
if (pDiskThread) pDiskThread->StopThread(); |
149 |
|
150 |
sleep(1); |
151 |
|
152 |
// free all resources |
153 |
if (pMidiInThread) delete pMidiInThread; |
154 |
if (pAudioThread) delete pAudioThread; |
155 |
if (pDiskThread) delete pDiskThread; |
156 |
if (pGig) delete pGig; |
157 |
if (pRIFF) delete pRIFF; |
158 |
if (pAudioIO) delete pAudioIO; |
159 |
|
160 |
printf("LinuxSampler stopped due to SIGINT\n"); |
161 |
exit(EXIT_SUCCESS); |
162 |
} |
163 |
} |
164 |
|
165 |
void parse_options(int argc, char **argv) { |
166 |
int res; |
167 |
int option_index = 0; |
168 |
static struct option long_options[] = |
169 |
{ |
170 |
{"numfragments",1,0,0}, |
171 |
{"fragmentsize",1,0,0}, |
172 |
{"dls",0,0,0}, |
173 |
{"gig",0,0,0}, |
174 |
{"help",0,0,0}, |
175 |
{0,0,0,0} |
176 |
}; |
177 |
|
178 |
while (true) { |
179 |
res = getopt_long_only(argc, argv, "", long_options, &option_index); |
180 |
if(res == -1) break; |
181 |
if (res == 0) { |
182 |
switch(option_index) { |
183 |
case 0: |
184 |
num_fragments = atoi(optarg); |
185 |
break; |
186 |
case 1: |
187 |
fragmentsize = atoi(optarg); |
188 |
break; |
189 |
case 2: |
190 |
patch_format = patch_format_dls; |
191 |
break; |
192 |
case 3: |
193 |
patch_format = patch_format_gig; |
194 |
break; |
195 |
case 4: |
196 |
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
197 |
printf("--numfragments sets the number of audio fragments\n"); |
198 |
printf("--fragmentsize sets the fragment size\n"); |
199 |
printf("--dls loads a DLS instrument\n"); |
200 |
printf("--gig loads a Gigasampler instrument\n"); |
201 |
exit(0); |
202 |
break; |
203 |
} |
204 |
} |
205 |
} |
206 |
} |