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