Imported Upstream version 3.0.4
[debian/gnuradio] / gr-audio-windows / src / audio_windows_sink.cc
1 /* -*- c++ -*- */
2 /*
3 * Copyright 2004 Free Software Foundation, Inc.
4 *
5 * This file is part of GNU Radio
6 *
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)
10 * any later version.
11 *
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.
16 *
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.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <audio_windows_sink.h>
28 #include <gr_io_signature.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <iostream>
35 #include <stdexcept>
36 #include <string>
37 #include <sstream>
38
39 static const double CHUNK_TIME = 0.1;   //0.001;           // 100 ms
40
41 // FIXME these should query some kind of user preference
42
43 static std::string
44 default_device_name ()
45 {
46   return "WAVE_MAPPER";
47 }
48
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)
56 {
57   d_wave_write_event = CreateEvent (NULL, FALSE, FALSE, NULL);
58   if (open_waveout_device () < 0)
59     {
60       //fprintf (stderr, "audio_windows_sink:open_waveout_device() failed\n");
61       perror ("audio_windows_sink:open_waveout_device( ) failed\n");
62       throw
63       std::runtime_error ("audio_windows_sink:open_waveout_device() failed");
64     }
65
66   d_chunk_size = (int) (d_sampling_freq * CHUNK_TIME);
67   set_output_multiple (d_chunk_size);
68
69   d_buffer = new short[d_chunk_size * 2];
70
71 }
72
73 audio_windows_sink::~audio_windows_sink ()
74 {
75   /* Free the callback Event */
76   CloseHandle (d_wave_write_event);
77   waveOutClose (d_h_waveout);
78   delete[]d_buffer;
79 }
80
81 audio_windows_sink_sptr
82 audio_windows_make_sink (int sampling_freq, const std::string dev)
83 {
84   return audio_windows_sink_sptr (new
85                                   audio_windows_sink (sampling_freq, dev));
86 }
87
88
89 int
90 audio_windows_sink::work (int noutput_items,
91                           gr_vector_const_void_star & input_items,
92                           gr_vector_void_star & output_items)
93 {
94   const float *f0, *f1;
95   bool playtestsound = false;
96   if (playtestsound)
97     {
98       // dummy
99
100       f0 = (const float *) input_items[0];
101
102       for (int i = 0; i < noutput_items; i += d_chunk_size)
103         {
104           for (int j = 0; j < d_chunk_size; j++)
105             {
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];
108             }
109           f0 += d_chunk_size;
110           if (write_waveout
111               ((HPSTR) d_buffer, 2 * d_chunk_size * sizeof (short)) < 0)
112             {
113               fprintf (stderr, "audio_windows_sink: write failed\n");
114               perror ("audio_windows_sink: write failed");
115             }
116         }
117       // break;
118     }
119   else
120     {
121       switch (input_items.size ())
122         {
123
124         case 1:         // mono input
125
126           f0 = (const float *) input_items[0];
127
128           for (int i = 0; i < noutput_items; i += d_chunk_size)
129             {
130               for (int j = 0; j < d_chunk_size; j++)
131                 {
132                   d_buffer[2 * j + 0] = (short) (f0[j] * 32767);
133                   d_buffer[2 * j + 1] = (short) (f0[j] * 32767);
134                 }
135               f0 += d_chunk_size;
136               if (write_waveout
137                   ((HPSTR) d_buffer, 2 * d_chunk_size * sizeof (short)) < 0)
138                 {
139                   //fprintf (stderr, "audio_windows_sink: write failed\n");
140                   perror ("audio_windows_sink: write failed");
141                 }
142             }
143           break;
144
145         case 2:         // stereo input
146
147           f0 = (const float *) input_items[0];
148           f1 = (const float *) input_items[1];
149
150           for (int i = 0; i < noutput_items; i += d_chunk_size)
151             {
152               for (int j = 0; j < d_chunk_size; j++)
153                 {
154                   d_buffer[2 * j + 0] = (short) (f0[j] * 32767);
155                   d_buffer[2 * j + 1] = (short) (f1[j] * 32767);
156                 }
157               f0 += d_chunk_size;
158               f1 += d_chunk_size;
159               if (write_waveout
160                   ((HPSTR) d_buffer, 2 * d_chunk_size * sizeof (short)) < 0)
161                 {
162                   //fprintf (stderr, "audio_windows_sink: write failed\n");
163                   perror ("audio_windows_sink: write failed");
164                 }
165             }
166           break;
167         }
168     }
169   return noutput_items;
170 }
171
172 int
173 audio_windows_sink::string_to_int (const std::string & s)
174 {
175   int i;
176   std::istringstream (s) >> i;
177   return i;
178 }                               //ToInt()
179
180 int
181 audio_windows_sink::open_waveout_device (void)
182 {
183
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.
186         *
187         * Value Meaning
188         * WAVE_MAPPER The function selects a waveform-audio output device capable of playing the given format.
189         */
190   if (d_device_name.empty () || default_device_name () == d_device_name)
191     u_device_id = WAVE_MAPPER;
192   else
193     u_device_id = (UINT) string_to_int (d_device_name);
194   // Open a waveform device for output using event callback.
195
196   unsigned long result;
197   //HWAVEOUT      outHandle;
198   WAVEFORMATEX wave_format;
199
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;
210
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
213   if (result)
214     {
215       fprintf (stderr,
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);
221       return -1;
222     }
223
224   //
225   // Do not Swallow the "open" event.
226   //
227   //WaitForSingleObject(d_wave_write_event, INFINITE);
228
229   // Allocate and lock memory for the header.
230
231   d_h_wave_hdr = GlobalAlloc (GMEM_MOVEABLE | GMEM_SHARE,
232                               (DWORD) sizeof (WAVEHDR));
233   if (d_h_wave_hdr == NULL)
234     {
235       //GlobalUnlock(hData);
236       //GlobalFree(hData);
237       //fprintf (stderr, "audio_windows_sink: Not enough memory for header.\n");
238       perror ("audio_windows_sink: Not enough memory for header.");
239       return -1;
240     }
241
242   d_lp_wave_hdr = (LPWAVEHDR) GlobalLock (d_h_wave_hdr);
243   if (d_lp_wave_hdr == NULL)
244     {
245       //GlobalUnlock(hData);
246       //GlobalFree(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.");
249       return -1;
250     }
251   //d_lp_wave_hdr->dwFlags = WHDR_DONE;
252   return 0;
253 }
254
255 int
256 audio_windows_sink::write_waveout (HPSTR lp_data, DWORD dw_data_size)
257 {
258   UINT w_result;
259   int teller = 100;
260   // After allocation, set up and prepare header.
261   /*while ((d_lp_wave_hdr->dwFlags & WHDR_DONE)==0 && teller>0)
262      {
263      teller--;
264      Sleep(1);
265      } */
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;
275
276   d_lp_wave_hdr->dwLoops = 0L;
277   w_result =
278     waveOutPrepareHeader (d_h_waveout, d_lp_wave_hdr, sizeof (WAVEHDR));
279   if (w_result != 0)
280     {
281       //GlobalUnlock( hData);
282       //GlobalFree(hData);
283       //fprintf (stderr, "audio_windows_sink: Failed to waveOutPrepareHeader. error %i\n",w_result);
284       perror ("audio_windows_sink: Failed to waveOutPrepareHeader");
285     }
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;
291   //
292   //
293
294   w_result = waveOutWrite (d_h_waveout, d_lp_wave_hdr, sizeof (WAVEHDR));
295   if (w_result != 0)
296     {
297       //GlobalUnlock( hData);
298       //GlobalFree(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");
301       switch (w_result)
302         {
303         case MMSYSERR_INVALHANDLE:
304           fprintf (stderr, "Specified device handle is invalid. \n");
305           break;
306         case MMSYSERR_NODRIVER:
307           fprintf (stderr, " No device driver is present.  \n");
308           break;
309         case MMSYSERR_NOMEM:
310           fprintf (stderr, " Unable to allocate or lock memory.  \n");
311           break;
312         case WAVERR_UNPREPARED:
313           fprintf (stderr,
314                    " The data block pointed to by the pwh parameter hasn't been prepared.  \n");
315           break;
316         default:
317           fprintf (stderr, "Unknown error %i\n", w_result);
318         }
319       waveOutUnprepareHeader (d_h_waveout, d_lp_wave_hdr, sizeof (WAVEHDR));
320       return -1;
321     }
322   //   WaitForSingleObject(d_wave_write_event, INFINITE);
323   return 0;
324 }