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

Diff of /linuxsampler/trunk/src/audioio.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 12 by schoenebeck, Wed Nov 5 14:47:10 2003 UTC revision 13 by schoenebeck, Fri Nov 21 15:07:23 2003 UTC
# Line 25  Line 25 
25  AudioIO::AudioIO() {  AudioIO::AudioIO() {
26      pcm_handle    = NULL;      pcm_handle    = NULL;
27      pOutputBuffer = NULL;      pOutputBuffer = NULL;
28        Initialized   = false;
29        stream        = SND_PCM_STREAM_PLAYBACK;
30  }  }
31    
32  AudioIO::~AudioIO() {  AudioIO::~AudioIO() {
# Line 32  AudioIO::~AudioIO() { Line 34  AudioIO::~AudioIO() {
34  }  }
35    
36  int AudioIO::Initialize(uint channels, uint samplerate, uint numfragments, uint fragmentsize) {  int AudioIO::Initialize(uint channels, uint samplerate, uint numfragments, uint fragmentsize) {
37      this->Channels         = channels;      this->Channels     = channels;
38      this->Samplerate       = samplerate;      this->Samplerate   = samplerate;
39      this->Fragments        = numfragments;      this->Fragments    = numfragments;
40      this->FragmentSize     = fragmentsize;      this->FragmentSize = fragmentsize;
41    
42      /* Playback stream */      if (HardwareParametersSupported(channels, samplerate, numfragments, fragmentsize)) {
43      snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;          pcm_name = "hw:0,0";
44        }
45      /* This structure contains information about    */      else {
46      /* the hardware and can be used to specify the  */          printf("Warning: your soundcard doesn't support chosen hardware parameters; ");
47      /* configuration to be used for the PCM stream. */          printf("trying to compensate support lack with plughw...");
48      snd_pcm_hw_params_t* hwparams;          fflush(stdout);
49            pcm_name = "plughw:0,0";
50      snd_pcm_sw_params_t* swparams;      }
   
     /* Name of the PCM device, like plughw:0,0          */  
     /* The first number is the number of the soundcard, */  
     /* the second number is the number of the device.   */  
     char* pcm_name;  
   
     /* Init pcm_name. Of course, later you */  
51    
52      int err;      int err;
53    
54      pcm_name = strdup("hw:0,0");      snd_pcm_hw_params_alloca(&hwparams);  // Allocate the snd_pcm_hw_params_t structure on the stack.
   
     /* Allocate the snd_pcm_hw_params_t structure on the stack. */  
     snd_pcm_hw_params_alloca(&hwparams);  
55    
56      /* Open PCM. The last parameter of this function is the mode. */      /* Open PCM. The last parameter of this function is the mode. */
57      /* If this is set to 0, the standard mode is used. Possible   */      /* If this is set to 0, the standard mode is used. Possible   */
# Line 68  int AudioIO::Initialize(uint channels, u Line 60  int AudioIO::Initialize(uint channels, u
60      /* PCM device will return immediately. If SND_PCM_ASYNC is    */      /* PCM device will return immediately. If SND_PCM_ASYNC is    */
61      /* specified, SIGIO will be emitted whenever a period has     */      /* specified, SIGIO will be emitted whenever a period has     */
62      /* been completely processed by the soundcard.                */      /* been completely processed by the soundcard.                */
63      if ((err = snd_pcm_open(&pcm_handle, pcm_name, stream, 0)) < 0) {      if ((err = snd_pcm_open(&pcm_handle, pcm_name.c_str(), stream, 0)) < 0) {
64          fprintf(stderr, "Error opening PCM device %s: %s\n", pcm_name, snd_strerror(err));          fprintf(stderr, "Error opening PCM device %s: %s\n", pcm_name.c_str(), snd_strerror(err));
65          return EXIT_FAILURE;          return EXIT_FAILURE;
66      }      }
67    
# Line 151  int AudioIO::Initialize(uint channels, u Line 143  int AudioIO::Initialize(uint channels, u
143    
144      // allocate the audio output buffer      // allocate the audio output buffer
145      pOutputBuffer = new int16_t[channels * fragmentsize];      pOutputBuffer = new int16_t[channels * fragmentsize];
146        
147        this->Initialized = true;
148    
149      return EXIT_SUCCESS;      return EXIT_SUCCESS;
150  }  }
151    
152    /**
153     *  Checks if sound card supports the chosen parameters.
154     *
155     *  @returns  true if hardware supports it
156     */
157    bool AudioIO::HardwareParametersSupported(uint channels, int samplerate, uint numfragments, uint fragmentsize) {
158        pcm_name = "hw:0,0";
159        if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), stream, 0) < 0) return false;
160        snd_pcm_hw_params_alloca(&hwparams);
161        if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
162            snd_pcm_close(pcm_handle);
163            return false;
164        }
165        if (snd_pcm_hw_params_test_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
166            snd_pcm_close(pcm_handle);
167            return false;
168        }
169        if (snd_pcm_hw_params_test_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE) < 0) {
170            snd_pcm_close(pcm_handle);
171            return false;
172        }
173        if (snd_pcm_hw_params_test_rate(pcm_handle, hwparams, samplerate, 0) < 0) {
174            snd_pcm_close(pcm_handle);
175            return false;
176        }
177        if (snd_pcm_hw_params_test_channels(pcm_handle, hwparams, channels) < 0) {
178            snd_pcm_close(pcm_handle);
179            return false;
180        }
181        if (snd_pcm_hw_params_test_periods(pcm_handle, hwparams, numfragments, 0) < 0) {
182            snd_pcm_close(pcm_handle);
183            return false;
184        }
185        if (snd_pcm_hw_params_test_buffer_size(pcm_handle, hwparams, (fragmentsize * numfragments)) < 0) {
186            snd_pcm_close(pcm_handle);
187            return false;
188        }
189    
190        snd_pcm_close(pcm_handle);
191        return true;
192    }
193    
194  int AudioIO::Output() {  int AudioIO::Output() {
195      int err = snd_pcm_writei(pcm_handle, pOutputBuffer, FragmentSize);      int err = snd_pcm_writei(pcm_handle, pOutputBuffer, FragmentSize);
196      if (err < 0) {      if (err < 0) {
# Line 165  int AudioIO::Output() { Line 201  int AudioIO::Output() {
201  }  }
202    
203  void AudioIO::Close(void) {  void AudioIO::Close(void) {
204      if (pcm_handle) {      if (Initialized) {
205          snd_pcm_close(pcm_handle);          if (pcm_handle) {
206          pcm_handle = NULL;              //FIXME: currently commented out due to segfault
207      }              //snd_pcm_close(pcm_handle);
208      if (pOutputBuffer) {              pcm_handle = NULL;
209          delete[] pOutputBuffer;          }
210          pOutputBuffer = NULL;          if (pOutputBuffer) {
211                //FIXME: currently commented out due to segfault
212                //delete[] pOutputBuffer;
213                pOutputBuffer = NULL;
214            }
215            Initialized = false;
216      }      }
217  }  }

Legend:
Removed from v.12  
changed lines
  Added in v.13

  ViewVC Help
Powered by ViewVC