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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 18 - (show annotations) (download)
Sun Dec 7 05:03:43 2003 UTC (20 years, 4 months ago) by schoenebeck
File size: 9135 byte(s)
* src/audioio.cpp: added support for Alsa 1.0.0
* src/audiothread.cpp: fixed several bugs in sustain pedal handling
* src/diskthread.cpp: fixed several bugs which occured under extreme
  conditions (endless loop in audiothread, freezing the whole application,
  outage of available disk streams)
* src/voice.cpp: fixed cubic interpolation (disabled by default; you can
  enable it by setting USE_LINEAR_INTERPOLATION to 0 in src/voice.h)
* src/configure.in: added check for Alsa version

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 "audioio.h"
24
25 AudioIO::AudioIO() {
26 pcm_handle = NULL;
27 pOutputBuffer = NULL;
28 Initialized = false;
29 stream = SND_PCM_STREAM_PLAYBACK;
30 }
31
32 AudioIO::~AudioIO() {
33 Close();
34 }
35
36 int AudioIO::Initialize(uint channels, uint samplerate, uint numfragments, uint fragmentsize) {
37 this->Channels = channels;
38 this->Samplerate = samplerate;
39 this->Fragments = numfragments;
40 this->FragmentSize = fragmentsize;
41
42 if (HardwareParametersSupported(channels, samplerate, numfragments, fragmentsize)) {
43 pcm_name = "hw:0,0";
44 }
45 else {
46 printf("Warning: your soundcard doesn't support chosen hardware parameters; ");
47 printf("trying to compensate support lack with plughw...");
48 fflush(stdout);
49 pcm_name = "plughw:0,0";
50 }
51
52 int err;
53
54 snd_pcm_hw_params_alloca(&hwparams); // Allocate the snd_pcm_hw_params_t structure on the stack.
55
56 /* Open PCM. The last parameter of this function is the mode. */
57 /* If this is set to 0, the standard mode is used. Possible */
58 /* other values are SND_PCM_NONBLOCK and SND_PCM_ASYNC. */
59 /* If SND_PCM_NONBLOCK is used, read / write access to the */
60 /* PCM device will return immediately. If SND_PCM_ASYNC is */
61 /* specified, SIGIO will be emitted whenever a period has */
62 /* been completely processed by the soundcard. */
63 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.c_str(), snd_strerror(err));
65 return EXIT_FAILURE;
66 }
67
68 if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0) {
69 fprintf(stderr, "Error, cannot initialize hardware parameter structure: %s.\n", snd_strerror(err));
70 return EXIT_FAILURE;
71 }
72
73 /* Set access type. This can be either */
74 /* SND_PCM_ACCESS_RW_INTERLEAVED or */
75 /* SND_PCM_ACCESS_RW_NONINTERLEAVED. */
76 if ((err = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
77 fprintf(stderr, "Error snd_pcm_hw_params_set_access: %s.\n", snd_strerror(err));
78 return EXIT_FAILURE;
79 }
80
81 /* Set sample format */
82 if ((err = snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE)) < 0) {
83 fprintf(stderr, "Error setting sample format. : %s\n", snd_strerror(err));
84 return EXIT_FAILURE;
85 }
86
87 int dir = 0;
88
89 /* Set sample rate. If the exact rate is not supported */
90 /* by the hardware, use nearest possible rate. */
91 #if ALSA_MAJOR > 0
92 if((err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &samplerate, &dir)) < 0) {
93 #else
94 if((err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, samplerate, &dir)) < 0) {
95 #endif
96 fprintf(stderr, "Error setting sample rate. : %s\n", snd_strerror(err));
97 return EXIT_FAILURE;
98 }
99
100 if ((err = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, channels)) < 0) {
101 fprintf(stderr, "Error setting number of channels. : %s\n", snd_strerror(err));
102 return EXIT_FAILURE;
103 }
104
105 /* Set number of periods. Periods used to be called fragments. */
106 if ((err = snd_pcm_hw_params_set_periods(pcm_handle, hwparams, numfragments, dir)) < 0) {
107 fprintf(stderr, "Error setting number of periods. : %s\n", snd_strerror(err));
108 return EXIT_FAILURE;
109 }
110
111 /* Set buffer size (in frames). The resulting latency is given by */
112 /* latency = periodsize * periods / (rate * bytes_per_frame) */
113 if ((err = snd_pcm_hw_params_set_buffer_size(pcm_handle, hwparams, (fragmentsize * numfragments))) < 0) {
114 fprintf(stderr, "Error setting buffersize. : %s\n", snd_strerror(err));
115 return EXIT_FAILURE;
116 }
117
118 /* Apply HW parameter settings to */
119 /* PCM device and prepare device */
120 if ((err = snd_pcm_hw_params(pcm_handle, hwparams)) < 0) {
121 fprintf(stderr, "Error setting HW params. : %s\n", snd_strerror(err));
122 return EXIT_FAILURE;
123 }
124
125 if (snd_pcm_sw_params_malloc(&swparams) != 0) {
126 fprintf(stderr, "Error in snd_pcm_sw_params_malloc. : %s\n", snd_strerror(err));
127 return EXIT_FAILURE;
128 }
129
130 if (snd_pcm_sw_params_current(pcm_handle, swparams) != 0) {
131 fprintf(stderr, "Error in snd_pcm_sw_params_current. : %s\n", snd_strerror(err));
132 return EXIT_FAILURE;
133 }
134
135 if (snd_pcm_sw_params_set_stop_threshold(pcm_handle, swparams, 0xffffffff) != 0) {
136 fprintf(stderr, "Error in snd_pcm_sw_params_set_stop_threshold. : %s\n", snd_strerror(err));
137 return EXIT_FAILURE;
138 }
139
140 if (snd_pcm_sw_params(pcm_handle, swparams) != 0) {
141 fprintf(stderr, "Error in snd_pcm_sw_params. : %s\n", snd_strerror(err));
142 return EXIT_FAILURE;
143 }
144
145 if ((err = snd_pcm_prepare(pcm_handle)) < 0) {
146 fprintf(stderr, "Error snd_pcm_prepare : %s\n", snd_strerror(err));
147 return EXIT_FAILURE;
148 }
149
150 // allocate the audio output buffer
151 pOutputBuffer = new int16_t[channels * fragmentsize];
152
153 this->Initialized = true;
154
155 return EXIT_SUCCESS;
156 }
157
158 /**
159 * Checks if sound card supports the chosen parameters.
160 *
161 * @returns true if hardware supports it
162 */
163 bool AudioIO::HardwareParametersSupported(uint channels, int samplerate, uint numfragments, uint fragmentsize) {
164 pcm_name = "hw:0,0";
165 if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), stream, 0) < 0) return false;
166 snd_pcm_hw_params_alloca(&hwparams);
167 if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
168 snd_pcm_close(pcm_handle);
169 return false;
170 }
171 if (snd_pcm_hw_params_test_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
172 snd_pcm_close(pcm_handle);
173 return false;
174 }
175 if (snd_pcm_hw_params_test_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE) < 0) {
176 snd_pcm_close(pcm_handle);
177 return false;
178 }
179 int dir = 0;
180 if (snd_pcm_hw_params_test_rate(pcm_handle, hwparams, samplerate, dir) < 0) {
181 snd_pcm_close(pcm_handle);
182 return false;
183 }
184 if (snd_pcm_hw_params_test_channels(pcm_handle, hwparams, channels) < 0) {
185 snd_pcm_close(pcm_handle);
186 return false;
187 }
188 if (snd_pcm_hw_params_test_periods(pcm_handle, hwparams, numfragments, dir) < 0) {
189 snd_pcm_close(pcm_handle);
190 return false;
191 }
192 if (snd_pcm_hw_params_test_buffer_size(pcm_handle, hwparams, (fragmentsize * numfragments)) < 0) {
193 snd_pcm_close(pcm_handle);
194 return false;
195 }
196
197 snd_pcm_close(pcm_handle);
198 return true;
199 }
200
201 /**
202 * Will be called by the audio engine after every audio fragment cycle, to
203 * output the audio data of the current fragment to the soundcard.
204 *
205 * @returns 0 on success
206 */
207 int AudioIO::Output() {
208 int err = snd_pcm_writei(pcm_handle, pOutputBuffer, FragmentSize);
209 if (err < 0) {
210 fprintf(stderr, "Error snd_pcm_writei failed. : %s\n", snd_strerror(err));
211 return EXIT_FAILURE;
212 }
213 return EXIT_SUCCESS;
214 }
215
216 void AudioIO::Close(void) {
217 if (Initialized) {
218 if (pcm_handle) {
219 //FIXME: currently commented out due to segfault
220 //snd_pcm_close(pcm_handle);
221 pcm_handle = NULL;
222 }
223 if (pOutputBuffer) {
224 //FIXME: currently commented out due to segfault
225 //delete[] pOutputBuffer;
226 pOutputBuffer = NULL;
227 }
228 Initialized = false;
229 }
230 }

  ViewVC Help
Powered by ViewVC