Imported Upstream version 3.0
[debian/gnuradio] / gr-audio-windows / src / audio_windows_source.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 2, 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_source.h>
28 #include <gr_io_signature.h>
29 //include <sys/soundcard.h>
30 //include <sys/ioctl.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <iostream>
37 #include <stdexcept>
38
39
40 static const double CHUNK_TIME = 0.005; // 5 ms
41
42 // FIXME these should query some kind of user preference
43
44 static std::string
45 default_device_name ()
46 {
47   return "/dev/dsp";
48 }
49
50 audio_windows_source::audio_windows_source (int sampling_freq, const std::string device_name)
51   : gr_sync_block ("audio_windows_source",
52                    gr_make_io_signature (0, 0, 0),
53                    gr_make_io_signature (1, 2, sizeof (float))),
54     d_sampling_freq (sampling_freq),
55     d_device_name (device_name.empty ()? default_device_name () : device_name),
56     d_fd (-1), d_buffer (0), d_chunk_size (0)
57 {
58   //FIXME TODO implement me
59 #if 0
60   if ((d_fd = open (d_device_name.c_str (), O_RDONLY)) < 0)
61     {
62       fprintf (stderr, "audio_windows_source: ");
63       perror (d_device_name.c_str ());
64       throw
65       std::runtime_error ("audio_windows_source");
66     }
67
68   d_chunk_size = (int) (d_sampling_freq * CHUNK_TIME);
69   set_output_multiple (d_chunk_size);
70
71   d_buffer = new short[d_chunk_size * 2];
72
73   int  format = AFMT_S16_NE;
74   int  orig_format = format;
75   if (ioctl (d_fd, SNDCTL_DSP_SETFMT, &format) < 0)
76     {
77       std::
78         cerr << "audio_windows_source: " << d_device_name <<
79         " ioctl failed\n";
80       perror (d_device_name.c_str ());
81       throw
82       std::runtime_error ("audio_windows_source");
83     }
84
85   if (format != orig_format)
86     {
87       fprintf (stderr, "audio_windows_source: unable to support format %d\n",
88                orig_format);
89       fprintf (stderr, "  card requested %d instead.\n", format);
90     }
91
92   // set to stereo no matter what.  Some hardware only does stereo
93   int channels = 2;
94   if (ioctl (d_fd, SNDCTL_DSP_CHANNELS, &channels) < 0 || channels != 2)
95     {
96       perror ("audio_windows_source: could not set STEREO mode");
97       throw
98       std::runtime_error ("audio_windows_source");
99     }
100
101   // set sampling freq
102   int sf = sampling_freq;
103   if (ioctl (d_fd, SNDCTL_DSP_SPEED, &sf) < 0)
104     {
105       std::cerr << "audio_windows_source: "
106         << d_device_name << ": invalid sampling_freq "
107         << sampling_freq << "\n";
108       sampling_freq = 8000;
109       if (ioctl (d_fd, SNDCTL_DSP_SPEED, &sf) < 0)
110         {
111           std::
112             cerr <<
113             "audio_windows_source: failed to set sampling_freq to 8000\n";
114           throw
115           std::runtime_error ("audio_windows_source");
116         }
117     }
118 #endif
119 }
120
121 audio_windows_source::~audio_windows_source ()
122 {
123   /*close (d_fd);
124      delete [] d_buffer;
125    */
126 }
127
128 audio_windows_source_sptr
129 audio_windows_make_source (int sampling_freq, const std::string dev)
130 {
131   return audio_windows_source_sptr (new
132                                     audio_windows_source (sampling_freq,
133                                                           dev));
134 }
135
136
137 int
138 audio_windows_source::work (int noutput_items,
139                             gr_vector_const_void_star & input_items,
140                             gr_vector_void_star & output_items)
141 {
142   //FIXME TODO implement me
143 #if 0
144   float *f0 = (float *) output_items[0];
145   float *f1 = (float *) output_items[1];        // will be invalid if this is mono output
146
147   const int shorts_per_item = 2;        // L + R
148   const int bytes_per_item = shorts_per_item * sizeof (short);
149
150   // To minimize latency, never return more than CHUNK_TIME
151   // worth of samples per call to work.
152   // FIXME, we need an API to set this value
153
154   noutput_items = std::min (noutput_items, d_chunk_size);
155
156   int base = 0;
157   int ntogo = noutput_items;
158
159   while (ntogo > 0)
160     {
161       int nbytes = std::min (ntogo, d_chunk_size) * bytes_per_item;
162       int result_nbytes = read (d_fd, d_buffer, nbytes);
163
164       if (result_nbytes < 0)
165         {
166           perror ("audio_windows_source");
167           return -1;            // say we're done
168         }
169
170       if ((result_nbytes & (bytes_per_item - 1)) != 0)
171         {
172           fprintf (stderr, "audio_windows_source: internal error.\n");
173           throw std::runtime_error ("internal error");
174         }
175
176       int result_nitems = result_nbytes / bytes_per_item;
177
178       // now unpack samples into output streams
179
180       switch (output_items.size ())
181         {
182         case 1:         // mono output
183           for (int i = 0; i < result_nitems; i++)
184             {
185               f0[base + i] = d_buffer[2 * i + 0] * (1.0 / 32767);
186             }
187           break;
188
189         case 2:         // stereo output
190           for (int i = 0; i < result_nitems; i++)
191             {
192               f0[base + i] = d_buffer[2 * i + 0] * (1.0 / 32767);
193               f1[base + i] = d_buffer[2 * i + 1] * (1.0 / 32767);
194             }
195           break;
196
197         default:
198           assert (0);
199         }
200
201       ntogo -= result_nitems;
202       base += result_nitems;
203     }
204
205   return noutput_items - ntogo;
206 #endif
207   return -1;                    // EOF
208 }