/[svn]/linuxsampler/trunk/src/linuxsampler.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/linuxsampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (show annotations) (download)
Wed Mar 31 10:28:42 2004 UTC (20 years ago) by schoenebeck
File size: 10973 byte(s)
removed unnecessary dependencies

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 <getopt.h>
24 #include <signal.h>
25
26 #include "global.h"
27 #include "diskthread.h"
28 #include "audiothread.h"
29 #include "alsaio.h"
30 #include "jackio.h"
31 #include "midiin.h"
32 #include "stream.h"
33 #include "RIFF.h"
34 #include "gig.h"
35 #include "network/lscpserver.h"
36
37 #define AUDIO_CHANNELS 2 // stereo
38 #define AUDIO_FRAGMENTS 3 // 3 fragments, if it does not work set it to 2
39 #define AUDIO_FRAGMENTSIZE 512 // each fragment has 512 frames
40 #define AUDIO_SAMPLERATE 44100 // Hz
41
42 enum patch_format_t {
43 patch_format_unknown,
44 patch_format_gig,
45 patch_format_dls
46 } patch_format = patch_format_unknown;
47
48 AudioIO* pAudioIO = NULL;
49 MidiIn* pMidiInThread = NULL;
50 LSCPServer* pLSCPServer = NULL;
51 AudioThread* pEngine = NULL;
52 uint instrument_index = 0;
53 double volume = 0.25;
54 int num_fragments = AUDIO_FRAGMENTS;
55 int fragmentsize = AUDIO_FRAGMENTSIZE;
56 uint samplerate = AUDIO_SAMPLERATE;
57 String input_client;
58 String alsaout = "0,0"; // default card
59 String jack_playback[2] = { "", "" };
60 bool use_jack = true;
61 bool run_server = false;
62 pthread_t signalhandlerthread;
63
64 void parse_options(int argc, char **argv);
65 void signal_handler(int signal);
66
67 int main(int argc, char **argv) {
68
69 // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
70 signalhandlerthread = pthread_self();
71 signal(SIGINT, signal_handler);
72
73 // parse and assign command line options
74 parse_options(argc, argv);
75
76 if (patch_format != patch_format_gig) {
77 printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n");
78 printf("Use 'linuxsampler --help' to see all available options.\n");
79 return EXIT_FAILURE;
80 }
81
82 int error = 1;
83 #if HAVE_JACK
84 if (use_jack) {
85 dmsg(1,("Initializing audio output (Jack)..."));
86 pAudioIO = new JackIO();
87 error = ((JackIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, jack_playback);
88 if (error) dmsg(1,("Trying Alsa output instead.\n"));
89 }
90 #endif // HAVE_JACK
91 if (error) {
92 dmsg(1,("Initializing audio output (Alsa)..."));
93 pAudioIO = new AlsaIO();
94 int error = ((AlsaIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, samplerate, num_fragments, fragmentsize, alsaout);
95 if (error) return EXIT_FAILURE;
96 }
97 dmsg(1,("OK\n"));
98
99 AudioThread* pEngine = new AudioThread(pAudioIO);
100 MidiIn* pMidiInThread = new MidiIn(pEngine);
101
102 // Loading gig file
103 result_t result = pEngine->LoadInstrument(argv[argc - 1], instrument_index);
104 if (result.type == result_type_error) return EXIT_FAILURE;
105 pEngine->Volume = volume;
106
107 dmsg(1,("Starting MIDI in thread..."));
108 if (input_client.size() > 0) pMidiInThread->SubscribeToClient(input_client.c_str());
109 pMidiInThread->StartThread();
110 dmsg(1,("OK\n"));
111
112 sleep(1);
113
114 dmsg(1,("Starting audio thread..."));
115 pAudioIO->AssignEngine(pEngine);
116 pAudioIO->Activate();
117 dmsg(1,("OK\n"));
118
119 if (run_server) {
120 dmsg(1,("Starting network server..."));
121 pLSCPServer = new LSCPServer(pEngine);
122 pLSCPServer->StartThread();
123 dmsg(1,("OK\n"));
124 }
125
126 printf("LinuxSampler initialization completed.\n");
127
128 while(true) {
129 printf("Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d, Unused: %3.3d)\r",
130 pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax,
131 pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams());
132 fflush(stdout);
133 usleep(500000);
134 }
135
136 return EXIT_SUCCESS;
137 }
138
139 void signal_handler(int signal) {
140 if (pthread_equal(pthread_self(), signalhandlerthread) && signal == SIGINT) {
141 // stop all threads
142 if (pAudioIO) pAudioIO->Close();
143 if (pMidiInThread) pMidiInThread->StopThread();
144
145 // free all resources
146 if (pMidiInThread) delete pMidiInThread;
147 if (pEngine) delete pEngine;
148 if (pAudioIO) delete pAudioIO;
149
150 printf("LinuxSampler stopped due to SIGINT\n");
151 exit(EXIT_SUCCESS);
152 }
153 }
154
155 void parse_options(int argc, char **argv) {
156 int res;
157 int option_index = 0;
158 static struct option long_options[] =
159 {
160 {"numfragments",1,0,0},
161 {"fragmentsize",1,0,0},
162 {"volume",1,0,0},
163 {"dls",0,0,0},
164 {"gig",0,0,0},
165 {"instrument",1,0,0},
166 {"inputclient",1,0,0},
167 {"alsaout",1,0,0},
168 {"jackout",1,0,0},
169 {"samplerate",1,0,0},
170 {"server",0,0,0},
171 {"help",0,0,0},
172 {0,0,0,0}
173 };
174
175 while (true) {
176 res = getopt_long_only(argc, argv, "", long_options, &option_index);
177 if(res == -1) break;
178 if (res == 0) {
179 switch(option_index) {
180 case 0: // --numfragments
181 num_fragments = atoi(optarg);
182 break;
183 case 1: // --fragmentsize
184 fragmentsize = atoi(optarg);
185 break;
186 case 2: // --volume
187 volume = atof(optarg);
188 break;
189 case 3: // --dls
190 patch_format = patch_format_dls;
191 break;
192 case 4: // --gig
193 patch_format = patch_format_gig;
194 break;
195 case 5: // --instrument
196 instrument_index = atoi(optarg);
197 break;
198 case 6: // --inputclient
199 input_client = optarg;
200 break;
201 case 7: // --alsaout
202 alsaout = optarg;
203 use_jack = false; // If this option is specified do not connect to jack
204 break;
205 case 8: { // --jackout
206 try {
207 String arg(optarg);
208 // remove outer apostrophes
209 arg = arg.substr(arg.find('\'') + 1, arg.rfind('\'') - (arg.find('\'') + 1));
210 // split in two arguments
211 jack_playback[0] = arg.substr(0, arg.find("\' "));
212 jack_playback[1] = arg.substr(arg.find("\' ") + 2, arg.size() - (arg.find("\' ") + 2));
213 // remove inner apostrophes
214 jack_playback[0] = jack_playback[0].substr(0, jack_playback[0].find('\''));
215 jack_playback[1] = jack_playback[1].substr(jack_playback[1].find('\'') + 1, jack_playback[1].size() - jack_playback[1].find('\''));
216 // this is the default but set it up anyway in case alsa_card was also used.
217 use_jack = true;
218 }
219 catch (...) {
220 fprintf(stderr, "Invalid argument '%s' for parameter --jackout\n", optarg);
221 exit(EXIT_FAILURE);
222 }
223 break;
224 }
225 case 9: // --samplerate
226 samplerate = atoi(optarg);
227 break;
228 case 10: // --server
229 run_server = true;
230 break;
231 case 11: // --help
232 printf("usage: linuxsampler [OPTIONS] <INSTRUMENTFILE>\n\n");
233 printf("--gig loads a Gigasampler instrument\n");
234 printf("--dls loads a DLS instrument\n");
235 printf("--instrument index of the instrument in the instrument file if it\n");
236 printf(" contains more than one (default: 0)\n");
237 printf("--numfragments sets the number of audio fragments\n");
238 printf("--fragmentsize sets the fragment size\n");
239 printf("--volume sets global volume gain factor (a value > 1.0 means\n");
240 printf(" amplification, a value < 1.0 means attenuation,\n");
241 printf(" default: 0.25)\n");
242 printf("--inputclient connects to an Alsa sequencer input client on startup\n");
243 printf(" (e.g. 64:0 to connect to a client with ID 64 and port 0)\n");
244 printf("--alsaout connects to the given Alsa sound device on startup\n");
245 printf(" (e.g. 0,0 to connect to hw:0,0 or plughw:0,0)\n");
246 printf("--jackout connects to the given Jack playback ports on startup\n");
247 printf(" (e.g. \"\'alsa_pcm:playback_1\' \'alsa_pcm:playback_2\'\"\n");
248 printf(" in case of stereo output)\n");
249 printf("--samplerate sets sample rate if supported by audio output system\n");
250 printf(" (e.g. 44100)\n");
251 printf("--server launch network server for remote control\n");
252 exit(EXIT_SUCCESS);
253 break;
254 }
255 }
256 }
257 }

  ViewVC Help
Powered by ViewVC