3 * Copyright 2004 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio 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 3, or (at your option)
12 * GNU Radio 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.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #include <audio_windows_sink.h>
28 #include <gr_io_signature.h>
29 #include <sys/types.h>
39 static const double CHUNK_TIME = 0.1; //0.001; // 100 ms
41 // FIXME these should query some kind of user preference
44 default_device_name ()
49 audio_windows_sink::audio_windows_sink (int sampling_freq, const std::string device_name)
50 : gr_sync_block ("audio_windows_sink",
51 gr_make_io_signature (1, 2, sizeof (float)),
52 gr_make_io_signature (0, 0, 0)),
53 d_sampling_freq (sampling_freq),
54 d_device_name (device_name.empty ()? default_device_name () : device_name),
55 d_fd (-1), d_buffer (0), d_chunk_size (0)
57 d_wave_write_event = CreateEvent (NULL, FALSE, FALSE, NULL);
58 if (open_waveout_device () < 0)
60 //fprintf (stderr, "audio_windows_sink:open_waveout_device() failed\n");
61 perror ("audio_windows_sink:open_waveout_device( ) failed\n");
63 std::runtime_error ("audio_windows_sink:open_waveout_device() failed");
66 d_chunk_size = (int) (d_sampling_freq * CHUNK_TIME);
67 set_output_multiple (d_chunk_size);
69 d_buffer = new short[d_chunk_size * 2];
73 audio_windows_sink::~audio_windows_sink ()
75 /* Free the callback Event */
76 CloseHandle (d_wave_write_event);
77 waveOutClose (d_h_waveout);
81 audio_windows_sink_sptr
82 audio_windows_make_sink (int sampling_freq, const std::string dev)
84 return audio_windows_sink_sptr (new
85 audio_windows_sink (sampling_freq, dev));
90 audio_windows_sink::work (int noutput_items,
91 gr_vector_const_void_star & input_items,
92 gr_vector_void_star & output_items)
95 bool playtestsound = false;
100 f0 = (const float *) input_items[0];
102 for (int i = 0; i < noutput_items; i += d_chunk_size)
104 for (int j = 0; j < d_chunk_size; j++)
106 d_buffer[2 * j + 0] = (short) (sin (2.0 * 3.1415926535897932384626 * (float) j * 1000.0 / (float) d_sampling_freq) * 8192 + 0); //+32767
107 d_buffer[2 * j + 1] = d_buffer[2 * j + 0];
111 ((HPSTR) d_buffer, 2 * d_chunk_size * sizeof (short)) < 0)
113 fprintf (stderr, "audio_windows_sink: write failed\n");
114 perror ("audio_windows_sink: write failed");
121 switch (input_items.size ())
124 case 1: // mono input
126 f0 = (const float *) input_items[0];
128 for (int i = 0; i < noutput_items; i += d_chunk_size)
130 for (int j = 0; j < d_chunk_size; j++)
132 d_buffer[2 * j + 0] = (short) (f0[j] * 32767);
133 d_buffer[2 * j + 1] = (short) (f0[j] * 32767);
137 ((HPSTR) d_buffer, 2 * d_chunk_size * sizeof (short)) < 0)
139 //fprintf (stderr, "audio_windows_sink: write failed\n");
140 perror ("audio_windows_sink: write failed");
145 case 2: // stereo input
147 f0 = (const float *) input_items[0];
148 f1 = (const float *) input_items[1];
150 for (int i = 0; i < noutput_items; i += d_chunk_size)
152 for (int j = 0; j < d_chunk_size; j++)
154 d_buffer[2 * j + 0] = (short) (f0[j] * 32767);
155 d_buffer[2 * j + 1] = (short) (f1[j] * 32767);
160 ((HPSTR) d_buffer, 2 * d_chunk_size * sizeof (short)) < 0)
162 //fprintf (stderr, "audio_windows_sink: write failed\n");
163 perror ("audio_windows_sink: write failed");
169 return noutput_items;
173 audio_windows_sink::string_to_int (const std::string & s)
176 std::istringstream (s) >> i;
181 audio_windows_sink::open_waveout_device (void)
184 UINT /*UINT_PTR */ u_device_id;
185 /** Identifier of the waveform-audio output device to open. It can be either a device identifier or a handle of an open waveform-audio input device. You can use the following flag instead of a device identifier.
188 * WAVE_MAPPER The function selects a waveform-audio output device capable of playing the given format.
190 if (d_device_name.empty () || default_device_name () == d_device_name)
191 u_device_id = WAVE_MAPPER;
193 u_device_id = (UINT) string_to_int (d_device_name);
194 // Open a waveform device for output using event callback.
196 unsigned long result;
197 //HWAVEOUT outHandle;
198 WAVEFORMATEX wave_format;
200 /* Initialize the WAVEFORMATEX for 16-bit, 44KHz, stereo */
201 wave_format.wFormatTag = WAVE_FORMAT_PCM;
202 wave_format.nChannels = 2;
203 wave_format.nSamplesPerSec = d_sampling_freq; //44100;
204 wave_format.wBitsPerSample = 16;
205 wave_format.nBlockAlign =
206 wave_format.nChannels * (wave_format.wBitsPerSample / 8);
207 wave_format.nAvgBytesPerSec =
208 wave_format.nSamplesPerSec * wave_format.nBlockAlign;
209 wave_format.cbSize = 0;
211 /* Open the (preferred) Digital Audio Out device. */
212 result = waveOutOpen (&d_h_waveout, WAVE_MAPPER, &wave_format, (DWORD_PTR) d_wave_write_event, 0, CALLBACK_EVENT | WAVE_ALLOWSYNC); //|WAVE_FORMAT_DIRECT | CALLBACK_EVENT| WAVE_ALLOWSYNC
216 "audio_windows_sink: Failed to open waveform output device.\n");
217 perror ("audio_windows_sink: Failed to open waveform output device.");
218 //LocalUnlock(hFormat);
219 //LocalFree(hFormat);
220 //mmioClose(hmmio, 0);
225 // Do not Swallow the "open" event.
227 //WaitForSingleObject(d_wave_write_event, INFINITE);
229 // Allocate and lock memory for the header.
231 d_h_wave_hdr = GlobalAlloc (GMEM_MOVEABLE | GMEM_SHARE,
232 (DWORD) sizeof (WAVEHDR));
233 if (d_h_wave_hdr == NULL)
235 //GlobalUnlock(hData);
237 //fprintf (stderr, "audio_windows_sink: Not enough memory for header.\n");
238 perror ("audio_windows_sink: Not enough memory for header.");
242 d_lp_wave_hdr = (LPWAVEHDR) GlobalLock (d_h_wave_hdr);
243 if (d_lp_wave_hdr == NULL)
245 //GlobalUnlock(hData);
247 //fprintf (stderr, "audio_windows_sink: Failed to lock memory for header.\n");
248 perror ("audio_windows_sink: Failed to lock memory for header.");
251 //d_lp_wave_hdr->dwFlags = WHDR_DONE;
256 audio_windows_sink::write_waveout (HPSTR lp_data, DWORD dw_data_size)
260 // After allocation, set up and prepare header.
261 /*while ((d_lp_wave_hdr->dwFlags & WHDR_DONE)==0 && teller>0)
266 // Wait until previous wave write completes (first event is the open event).
267 WaitForSingleObject (d_wave_write_event, 100); //INFINITE
268 d_lp_wave_hdr->lpData = lp_data;
269 d_lp_wave_hdr->dwBufferLength = dw_data_size;
270 d_lp_wave_hdr->dwFlags = 0L;
271 /* Clear the WHDR_DONE bit (which the driver set last time that
272 this WAVEHDR was sent via waveOutWrite and was played). Some
273 drivers need this to be cleared */
274 //d_lp_wave_hdr->dwFlags &= ~WHDR_DONE;
276 d_lp_wave_hdr->dwLoops = 0L;
278 waveOutPrepareHeader (d_h_waveout, d_lp_wave_hdr, sizeof (WAVEHDR));
281 //GlobalUnlock( hData);
283 //fprintf (stderr, "audio_windows_sink: Failed to waveOutPrepareHeader. error %i\n",w_result);
284 perror ("audio_windows_sink: Failed to waveOutPrepareHeader");
286 // Now the data block can be sent to the output device. The
287 // waveOutWrite function returns immediately and waveform
288 // data is sent to the output device in the background.
289 //while (! readyforplayback) Sleep(1);
290 //readyforplayback=false;
294 w_result = waveOutWrite (d_h_waveout, d_lp_wave_hdr, sizeof (WAVEHDR));
297 //GlobalUnlock( hData);
299 //fprintf (stderr, "audio_windows_sink: Failed to write block to device.error %i\n",w_result);
300 perror ("audio_windows_sink: Failed to write block to device");
303 case MMSYSERR_INVALHANDLE:
304 fprintf (stderr, "Specified device handle is invalid. \n");
306 case MMSYSERR_NODRIVER:
307 fprintf (stderr, " No device driver is present. \n");
310 fprintf (stderr, " Unable to allocate or lock memory. \n");
312 case WAVERR_UNPREPARED:
314 " The data block pointed to by the pwh parameter hasn't been prepared. \n");
317 fprintf (stderr, "Unknown error %i\n", w_result);
319 waveOutUnprepareHeader (d_h_waveout, d_lp_wave_hdr, sizeof (WAVEHDR));
322 // WaitForSingleObject(d_wave_write_event, INFINITE);