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 2, 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_oss_sink.h>
28 #include <gr_io_signature.h>
30 #include <sys/soundcard.h>
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
42 default_device_name ()
44 return gr_prefs::singleton()->get_string("audio_oss", "default_output_device", "/dev/dsp");
48 audio_oss_make_sink (int sampling_rate, const std::string dev, bool ok_to_block)
50 return audio_oss_sink_sptr (new audio_oss_sink (sampling_rate, dev, ok_to_block));
53 audio_oss_sink::audio_oss_sink (int sampling_rate,
54 const std::string device_name,
56 : gr_sync_block ("audio_oss_sink",
57 gr_make_io_signature (1, 2, sizeof (float)),
58 gr_make_io_signature (0, 0, 0)),
59 d_sampling_rate (sampling_rate),
60 d_device_name (device_name.empty() ? default_device_name() : device_name),
61 d_fd (-1), d_buffer (0), d_chunk_size (0)
63 if ((d_fd = open (d_device_name.c_str (), O_WRONLY)) < 0){
64 fprintf (stderr, "audio_oss_sink: ");
65 perror (d_device_name.c_str ());
66 throw std::runtime_error ("audio_oss_sink");
70 std::max(0.001, gr_prefs::singleton()->get_double("audio_oss", "latency", 0.005));
72 d_chunk_size = (int) (d_sampling_rate * CHUNK_TIME);
73 set_output_multiple (d_chunk_size);
75 d_buffer = new short [d_chunk_size * 2];
77 int format = AFMT_S16_NE;
78 int orig_format = format;
79 if (ioctl (d_fd, SNDCTL_DSP_SETFMT, &format) < 0){
80 std::cerr << "audio_oss_sink: " << d_device_name << " ioctl failed\n";
81 perror (d_device_name.c_str ());
82 throw std::runtime_error ("audio_oss_sink");
85 if (format != orig_format){
86 fprintf (stderr, "audio_oss_sink: unable to support format %d\n", orig_format);
87 fprintf (stderr, " card requested %d instead.\n", format);
90 // set to stereo no matter what. Some hardware only does stereo
92 if (ioctl (d_fd, SNDCTL_DSP_CHANNELS, &channels) < 0 || channels != 2){
93 perror ("audio_oss_sink: could not set STEREO mode");
94 throw std::runtime_error ("audio_oss_sink");
98 int sf = sampling_rate;
99 if (ioctl (d_fd, SNDCTL_DSP_SPEED, &sf) < 0){
100 std::cerr << "audio_oss_sink: "
101 << d_device_name << ": invalid sampling_rate "
102 << sampling_rate << "\n";
103 sampling_rate = 8000;
104 if (ioctl (d_fd, SNDCTL_DSP_SPEED, &sf) < 0){
105 std::cerr << "audio_oss_sink: failed to set sampling_rate to 8000\n";
106 throw std::runtime_error ("audio_oss_sink");
111 audio_oss_sink::~audio_oss_sink ()
119 audio_oss_sink::work (int noutput_items,
120 gr_vector_const_void_star &input_items,
121 gr_vector_void_star &output_items)
123 const float *f0, *f1;
125 switch (input_items.size ()){
127 case 1: // mono input
129 f0 = (const float *) input_items[0];
131 for (int i = 0; i < noutput_items; i += d_chunk_size){
132 for (int j = 0; j < d_chunk_size; j++){
133 d_buffer[2*j+0] = (short) (f0[j] * 32767);
134 d_buffer[2*j+1] = (short) (f0[j] * 32767);
137 if (write (d_fd, d_buffer, 2 * d_chunk_size * sizeof (short)) < 0)
138 perror ("audio_oss_sink: write");
142 case 2: // stereo input
144 f0 = (const float *) input_items[0];
145 f1 = (const float *) input_items[1];
147 for (int i = 0; i < noutput_items; i += d_chunk_size){
148 for (int j = 0; j < d_chunk_size; j++){
149 d_buffer[2*j+0] = (short) (f0[j] * 32767);
150 d_buffer[2*j+1] = (short) (f1[j] * 32767);
154 if (write (d_fd, d_buffer, 2 * d_chunk_size * sizeof (short)) < 0)
155 perror ("audio_oss_sink: write");
160 return noutput_items;