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_source.h>
28 #include <gr_io_signature.h>
29 //include <sys/soundcard.h>
30 //include <sys/ioctl.h>
31 #include <sys/types.h>
40 static const double CHUNK_TIME = 0.005; // 5 ms
42 // FIXME these should query some kind of user preference
45 default_device_name ()
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)
58 //FIXME TODO implement me
60 if ((d_fd = open (d_device_name.c_str (), O_RDONLY)) < 0)
62 fprintf (stderr, "audio_windows_source: ");
63 perror (d_device_name.c_str ());
65 std::runtime_error ("audio_windows_source");
68 d_chunk_size = (int) (d_sampling_freq * CHUNK_TIME);
69 set_output_multiple (d_chunk_size);
71 d_buffer = new short[d_chunk_size * 2];
73 int format = AFMT_S16_NE;
74 int orig_format = format;
75 if (ioctl (d_fd, SNDCTL_DSP_SETFMT, &format) < 0)
78 cerr << "audio_windows_source: " << d_device_name <<
80 perror (d_device_name.c_str ());
82 std::runtime_error ("audio_windows_source");
85 if (format != orig_format)
87 fprintf (stderr, "audio_windows_source: unable to support format %d\n",
89 fprintf (stderr, " card requested %d instead.\n", format);
92 // set to stereo no matter what. Some hardware only does stereo
94 if (ioctl (d_fd, SNDCTL_DSP_CHANNELS, &channels) < 0 || channels != 2)
96 perror ("audio_windows_source: could not set STEREO mode");
98 std::runtime_error ("audio_windows_source");
102 int sf = sampling_freq;
103 if (ioctl (d_fd, SNDCTL_DSP_SPEED, &sf) < 0)
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)
113 "audio_windows_source: failed to set sampling_freq to 8000\n";
115 std::runtime_error ("audio_windows_source");
121 audio_windows_source::~audio_windows_source ()
128 audio_windows_source_sptr
129 audio_windows_make_source (int sampling_freq, const std::string dev)
131 return audio_windows_source_sptr (new
132 audio_windows_source (sampling_freq,
138 audio_windows_source::work (int noutput_items,
139 gr_vector_const_void_star & input_items,
140 gr_vector_void_star & output_items)
142 //FIXME TODO implement me
144 float *f0 = (float *) output_items[0];
145 float *f1 = (float *) output_items[1]; // will be invalid if this is mono output
147 const int shorts_per_item = 2; // L + R
148 const int bytes_per_item = shorts_per_item * sizeof (short);
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
154 noutput_items = std::min (noutput_items, d_chunk_size);
157 int ntogo = noutput_items;
161 int nbytes = std::min (ntogo, d_chunk_size) * bytes_per_item;
162 int result_nbytes = read (d_fd, d_buffer, nbytes);
164 if (result_nbytes < 0)
166 perror ("audio_windows_source");
167 return -1; // say we're done
170 if ((result_nbytes & (bytes_per_item - 1)) != 0)
172 fprintf (stderr, "audio_windows_source: internal error.\n");
173 throw std::runtime_error ("internal error");
176 int result_nitems = result_nbytes / bytes_per_item;
178 // now unpack samples into output streams
180 switch (output_items.size ())
182 case 1: // mono output
183 for (int i = 0; i < result_nitems; i++)
185 f0[base + i] = d_buffer[2 * i + 0] * (1.0 / 32767);
189 case 2: // stereo output
190 for (int i = 0; i < result_nitems; i++)
192 f0[base + i] = d_buffer[2 * i + 0] * (1.0 / 32767);
193 f1[base + i] = d_buffer[2 * i + 1] * (1.0 / 32767);
201 ntogo -= result_nitems;
202 base += result_nitems;
205 return noutput_items - ntogo;