1 |
schoenebeck |
9 |
/*************************************************************************** |
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) sleep(1000); |
136 |
|
|
return EXIT_SUCCESS; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
void signal_handler(int signal) { |
140 |
|
|
if (signal == SIGINT) { |
141 |
|
|
// stop all threads |
142 |
|
|
if (pMidiInThread) pMidiInThread->StopThread(); |
143 |
|
|
if (pAudioThread) pAudioThread->StopThread(); |
144 |
|
|
if (pDiskThread) pDiskThread->StopThread(); |
145 |
|
|
|
146 |
|
|
sleep(1); |
147 |
|
|
|
148 |
|
|
// free all resources |
149 |
|
|
if (pMidiInThread) delete pMidiInThread; |
150 |
|
|
if (pAudioThread) delete pAudioThread; |
151 |
|
|
if (pDiskThread) delete pDiskThread; |
152 |
|
|
if (pGig) delete pGig; |
153 |
|
|
if (pRIFF) delete pRIFF; |
154 |
|
|
if (pAudioIO) delete pAudioIO; |
155 |
|
|
|
156 |
|
|
printf("LinuxSampler stopped due to SIGINT\n"); |
157 |
|
|
exit(EXIT_SUCCESS); |
158 |
|
|
} |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
void parse_options(int argc, char **argv) { |
162 |
|
|
int res; |
163 |
|
|
int option_index = 0; |
164 |
|
|
static struct option long_options[] = |
165 |
|
|
{ |
166 |
|
|
{"numfragments",1,0,0}, |
167 |
|
|
{"fragmentsize",1,0,0}, |
168 |
|
|
{"dls",0,0,0}, |
169 |
|
|
{"gig",0,0,0}, |
170 |
|
|
{"help",0,0,0}, |
171 |
|
|
{0,0,0,0} |
172 |
|
|
}; |
173 |
|
|
|
174 |
|
|
while (true) { |
175 |
|
|
res = getopt_long_only(argc, argv, "", long_options, &option_index); |
176 |
|
|
if(res == -1) break; |
177 |
|
|
if (res == 0) { |
178 |
|
|
switch(option_index) { |
179 |
|
|
case 0: |
180 |
|
|
num_fragments = atoi(optarg); |
181 |
|
|
break; |
182 |
|
|
case 1: |
183 |
|
|
fragmentsize = atoi(optarg); |
184 |
|
|
break; |
185 |
|
|
case 2: |
186 |
|
|
patch_format = patch_format_dls; |
187 |
|
|
break; |
188 |
|
|
case 3: |
189 |
|
|
patch_format = patch_format_gig; |
190 |
|
|
break; |
191 |
|
|
case 4: |
192 |
|
|
printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n"); |
193 |
|
|
printf("--numfragments sets the number of audio fragments\n"); |
194 |
|
|
printf("--fragmentsize sets the fragment size\n"); |
195 |
|
|
printf("--dls loads a DLS instrument\n"); |
196 |
|
|
printf("--gig loads a Gigasampler instrument\n"); |
197 |
|
|
exit(0); |
198 |
|
|
break; |
199 |
|
|
} |
200 |
|
|
} |
201 |
|
|
} |
202 |
|
|
} |