Imported Upstream version 3.0
[debian/gnuradio] / gr-audio-oss / src / audio_oss_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 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_oss_sink.h>
28 #include <gr_io_signature.h>
29 #include <gr_prefs.h>
30 #include <sys/soundcard.h>
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <stdio.h>
37 #include <iostream>
38 #include <stdexcept>
39
40
41 static std::string
42 default_device_name ()
43 {
44   return gr_prefs::singleton()->get_string("audio_oss", "default_output_device", "/dev/dsp");
45 }
46
47 audio_oss_sink_sptr
48 audio_oss_make_sink (int sampling_rate, const std::string dev, bool ok_to_block)
49 {
50   return audio_oss_sink_sptr (new audio_oss_sink (sampling_rate, dev, ok_to_block));
51 }
52
53 audio_oss_sink::audio_oss_sink (int sampling_rate,
54                                 const std::string device_name,
55                                 bool ok_to_block)
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)
62 {
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");
67   }
68
69   double CHUNK_TIME = 
70     std::max(0.001, gr_prefs::singleton()->get_double("audio_oss", "latency", 0.005));
71
72   d_chunk_size = (int) (d_sampling_rate * CHUNK_TIME);
73   set_output_multiple (d_chunk_size);
74
75   d_buffer = new short [d_chunk_size * 2];
76
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");
83   }
84
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);
88   }
89
90   // set to stereo no matter what.  Some hardware only does stereo
91   int channels = 2;
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");
95   }
96
97   // set sampling freq
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");
107     }
108   }
109 }
110
111 audio_oss_sink::~audio_oss_sink ()
112 {
113   close (d_fd);
114   delete [] d_buffer;
115 }
116
117
118 int
119 audio_oss_sink::work (int noutput_items,
120                       gr_vector_const_void_star &input_items,
121                       gr_vector_void_star &output_items)
122 {
123   const float  *f0, *f1;
124   
125   switch (input_items.size ()){
126
127   case 1:               // mono input
128
129     f0 = (const float *) input_items[0];
130
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);
135       }
136       f0 += d_chunk_size;
137       if (write (d_fd, d_buffer, 2 * d_chunk_size * sizeof (short)) < 0)
138         perror ("audio_oss_sink: write");
139     }
140     break;
141     
142   case 2:               // stereo input
143
144     f0 = (const float *) input_items[0];
145     f1 = (const float *) input_items[1];
146
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);
151       }
152       f0 += d_chunk_size;
153       f1 += d_chunk_size;
154       if (write (d_fd, d_buffer, 2 * d_chunk_size * sizeof (short)) < 0)
155         perror ("audio_oss_sink: write");
156     }
157     break;
158   }
159
160   return noutput_items;
161 }