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

Annotation of /linuxsampler/trunk/src/linuxsampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (hide annotations) (download)
Sun Nov 16 19:01:50 2003 UTC (20 years, 5 months ago) by schoenebeck
File size: 6962 byte(s)
* src/gig.cpp: fixed bug in decompression algorithm which caused it not to
  detect the end of a stream and let the disk streams reload forever also
  resulting in strange sounds at the end of disk voices (concerned only
  playback of compressed gig files)
* src/audiothread.cpp: deallocation of voices when they reached the end of
  playback (thus e.g. when sustain pedal is pressed and a disk stream
  reached it's end)
* various endian corrections needed for non intel systems
* introduced debug level, you can set the debug level and thus the
  verbosity of LinuxSampler in src/global.h

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     RIFF::File* pRIFF;
54     gig::File* pGig;
55     gig::Instrument* pInstrument;
56 schoenebeck 12 int num_fragments;
57     int fragmentsize;
58 schoenebeck 9
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 schoenebeck 12 printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n");
79 schoenebeck 9 return EXIT_FAILURE;
80     }
81    
82 schoenebeck 12 dmsg(1,("Initializing audio output..."));
83 schoenebeck 9 pAudioIO = new AudioIO();
84     int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize);
85     if (error) return EXIT_FAILURE;
86 schoenebeck 12 dmsg(1,("OK\n"));
87 schoenebeck 9
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 schoenebeck 12 dmsg(1,("Starting disk thread..."));
113 schoenebeck 9 pDiskThread->StartThread();
114 schoenebeck 12 dmsg(1,("OK\n"));
115     dmsg(1,("Starting MIDI in thread..."));
116 schoenebeck 9 pMidiInThread->StartThread();
117 schoenebeck 12 dmsg(1,("OK\n"));
118 schoenebeck 9
119     sleep(1);
120 schoenebeck 12 dmsg(1,("Starting audio thread..."));
121 schoenebeck 9 pAudioThread->StartThread();
122 schoenebeck 12 dmsg(1,("OK\n"));
123 schoenebeck 9
124     printf("LinuxSampler initialization completed.\n");
125    
126 senoner 10 while(true) {
127     printf("Voices: %3.3d Streams: %3.3d \r",pAudioThread->ActiveVoiceCount, pDiskThread->ActiveStreamCount); fflush(stdout);
128     usleep(500000);
129     }
130    
131 schoenebeck 9 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     }

  ViewVC Help
Powered by ViewVC