summaryrefslogtreecommitdiff
path: root/gr-audio/lib/oss
diff options
context:
space:
mode:
Diffstat (limited to 'gr-audio/lib/oss')
-rw-r--r--gr-audio/lib/oss/audio_oss_sink.cc161
-rw-r--r--gr-audio/lib/oss/audio_oss_sink.h54
-rw-r--r--gr-audio/lib/oss/audio_oss_source.cc177
-rw-r--r--gr-audio/lib/oss/audio_oss_source.h58
-rw-r--r--gr-audio/lib/oss/gr-audio-oss.conf9
5 files changed, 459 insertions, 0 deletions
diff --git a/gr-audio/lib/oss/audio_oss_sink.cc b/gr-audio/lib/oss/audio_oss_sink.cc
new file mode 100644
index 000000000..a33e569f6
--- /dev/null
+++ b/gr-audio/lib/oss/audio_oss_sink.cc
@@ -0,0 +1,161 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2004-2011 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "gr_audio_registry.h"
+#include <audio_oss_sink.h>
+#include <gr_io_signature.h>
+#include <gr_prefs.h>
+#include <sys/soundcard.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <iostream>
+#include <stdexcept>
+
+AUDIO_REGISTER_SINK(oss)(
+ int sampling_rate, const std::string &device_name, bool ok_to_block
+){
+ return audio_sink::sptr(new audio_oss_sink(sampling_rate, device_name, ok_to_block));
+}
+
+static std::string
+default_device_name ()
+{
+ return gr_prefs::singleton()->get_string("audio_oss", "default_output_device", "/dev/dsp");
+}
+
+audio_oss_sink::audio_oss_sink (int sampling_rate,
+ const std::string device_name,
+ bool ok_to_block)
+ : audio_sink ("audio_oss_sink",
+ gr_make_io_signature (1, 2, sizeof (float)),
+ gr_make_io_signature (0, 0, 0)),
+ d_sampling_rate (sampling_rate),
+ d_device_name (device_name.empty() ? default_device_name() : device_name),
+ d_fd (-1), d_buffer (0), d_chunk_size (0)
+{
+ if ((d_fd = open (d_device_name.c_str (), O_WRONLY)) < 0){
+ fprintf (stderr, "audio_oss_sink: ");
+ perror (d_device_name.c_str ());
+ throw std::runtime_error ("audio_oss_sink");
+ }
+
+ double CHUNK_TIME =
+ std::max(0.001, gr_prefs::singleton()->get_double("audio_oss", "latency", 0.005));
+
+ d_chunk_size = (int) (d_sampling_rate * CHUNK_TIME);
+ set_output_multiple (d_chunk_size);
+
+ d_buffer = new short [d_chunk_size * 2];
+
+ int format = AFMT_S16_NE;
+ int orig_format = format;
+ if (ioctl (d_fd, SNDCTL_DSP_SETFMT, &format) < 0){
+ std::cerr << "audio_oss_sink: " << d_device_name << " ioctl failed\n";
+ perror (d_device_name.c_str ());
+ throw std::runtime_error ("audio_oss_sink");
+ }
+
+ if (format != orig_format){
+ fprintf (stderr, "audio_oss_sink: unable to support format %d\n", orig_format);
+ fprintf (stderr, " card requested %d instead.\n", format);
+ }
+
+ // set to stereo no matter what. Some hardware only does stereo
+ int channels = 2;
+ if (ioctl (d_fd, SNDCTL_DSP_CHANNELS, &channels) < 0 || channels != 2){
+ perror ("audio_oss_sink: could not set STEREO mode");
+ throw std::runtime_error ("audio_oss_sink");
+ }
+
+ // set sampling freq
+ int sf = sampling_rate;
+ if (ioctl (d_fd, SNDCTL_DSP_SPEED, &sf) < 0){
+ std::cerr << "audio_oss_sink: "
+ << d_device_name << ": invalid sampling_rate "
+ << sampling_rate << "\n";
+ sampling_rate = 8000;
+ if (ioctl (d_fd, SNDCTL_DSP_SPEED, &sf) < 0){
+ std::cerr << "audio_oss_sink: failed to set sampling_rate to 8000\n";
+ throw std::runtime_error ("audio_oss_sink");
+ }
+ }
+}
+
+audio_oss_sink::~audio_oss_sink ()
+{
+ close (d_fd);
+ delete [] d_buffer;
+}
+
+
+int
+audio_oss_sink::work (int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items)
+{
+ const float *f0, *f1;
+
+ switch (input_items.size ()){
+
+ case 1: // mono input
+
+ f0 = (const float *) input_items[0];
+
+ for (int i = 0; i < noutput_items; i += d_chunk_size){
+ for (int j = 0; j < d_chunk_size; j++){
+ d_buffer[2*j+0] = (short) (f0[j] * 32767);
+ d_buffer[2*j+1] = (short) (f0[j] * 32767);
+ }
+ f0 += d_chunk_size;
+ if (write (d_fd, d_buffer, 2 * d_chunk_size * sizeof (short)) < 0)
+ perror ("audio_oss_sink: write");
+ }
+ break;
+
+ case 2: // stereo input
+
+ f0 = (const float *) input_items[0];
+ f1 = (const float *) input_items[1];
+
+ for (int i = 0; i < noutput_items; i += d_chunk_size){
+ for (int j = 0; j < d_chunk_size; j++){
+ d_buffer[2*j+0] = (short) (f0[j] * 32767);
+ d_buffer[2*j+1] = (short) (f1[j] * 32767);
+ }
+ f0 += d_chunk_size;
+ f1 += d_chunk_size;
+ if (write (d_fd, d_buffer, 2 * d_chunk_size * sizeof (short)) < 0)
+ perror ("audio_oss_sink: write");
+ }
+ break;
+ }
+
+ return noutput_items;
+}
diff --git a/gr-audio/lib/oss/audio_oss_sink.h b/gr-audio/lib/oss/audio_oss_sink.h
new file mode 100644
index 000000000..0d7280c2f
--- /dev/null
+++ b/gr-audio/lib/oss/audio_oss_sink.h
@@ -0,0 +1,54 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2004-2011 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef INCLUDED_AUDIO_OSS_SINK_H
+#define INCLUDED_AUDIO_OSS_SINK_H
+
+#include <gr_audio_sink.h>
+#include <string>
+
+/*!
+ * \brief audio sink using OSS
+ *
+ * input signature is one or two streams of floats.
+ * Input samples must be in the range [-1,1].
+ */
+
+class audio_oss_sink : public audio_sink {
+
+ int d_sampling_rate;
+ std::string d_device_name;
+ int d_fd;
+ short *d_buffer;
+ int d_chunk_size;
+
+public:
+ audio_oss_sink (int sampling_rate, const std::string device_name = "", bool ok_to_block = true);
+
+ ~audio_oss_sink ();
+
+ int work (int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items);
+};
+
+#endif /* INCLUDED_AUDIO_OSS_SINK_H */
diff --git a/gr-audio/lib/oss/audio_oss_source.cc b/gr-audio/lib/oss/audio_oss_source.cc
new file mode 100644
index 000000000..f52af1bb8
--- /dev/null
+++ b/gr-audio/lib/oss/audio_oss_source.cc
@@ -0,0 +1,177 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2004-2011 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "gr_audio_registry.h"
+#include <audio_oss_source.h>
+#include <gr_io_signature.h>
+#include <gr_prefs.h>
+#include <sys/soundcard.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <iostream>
+#include <stdexcept>
+
+AUDIO_REGISTER_SOURCE(oss)(
+ int sampling_rate, const std::string &device_name, bool ok_to_block
+){
+ return audio_source::sptr(new audio_oss_source(sampling_rate, device_name, ok_to_block));
+}
+
+static std::string
+default_device_name ()
+{
+ return gr_prefs::singleton()->get_string("audio_oss", "default_input_device", "/dev/dsp");
+}
+
+audio_oss_source::audio_oss_source (int sampling_rate,
+ const std::string device_name,
+ bool ok_to_block)
+ : audio_source ("audio_oss_source",
+ gr_make_io_signature (0, 0, 0),
+ gr_make_io_signature (1, 2, sizeof (float))),
+ d_sampling_rate (sampling_rate),
+ d_device_name (device_name.empty() ? default_device_name() : device_name),
+ d_fd (-1), d_buffer (0), d_chunk_size (0)
+{
+ if ((d_fd = open (d_device_name.c_str (), O_RDONLY)) < 0){
+ fprintf (stderr, "audio_oss_source: ");
+ perror (d_device_name.c_str ());
+ throw std::runtime_error ("audio_oss_source");
+ }
+
+ double CHUNK_TIME =
+ std::max(0.001, gr_prefs::singleton()->get_double("audio_oss", "latency", 0.005));
+
+ d_chunk_size = (int) (d_sampling_rate * CHUNK_TIME);
+ set_output_multiple (d_chunk_size);
+
+ d_buffer = new short [d_chunk_size * 2];
+
+ int format = AFMT_S16_NE;
+ int orig_format = format;
+ if (ioctl (d_fd, SNDCTL_DSP_SETFMT, &format) < 0){
+ std::cerr << "audio_oss_source: " << d_device_name << " ioctl failed\n";
+ perror (d_device_name.c_str ());
+ throw std::runtime_error ("audio_oss_source");
+ }
+
+ if (format != orig_format){
+ fprintf (stderr, "audio_oss_source: unable to support format %d\n", orig_format);
+ fprintf (stderr, " card requested %d instead.\n", format);
+ }
+
+ // set to stereo no matter what. Some hardware only does stereo
+ int channels = 2;
+ if (ioctl (d_fd, SNDCTL_DSP_CHANNELS, &channels) < 0 || channels != 2){
+ perror ("audio_oss_source: could not set STEREO mode");
+ throw std::runtime_error ("audio_oss_source");
+ }
+
+ // set sampling freq
+ int sf = sampling_rate;
+ if (ioctl (d_fd, SNDCTL_DSP_SPEED, &sf) < 0){
+ std::cerr << "audio_oss_source: "
+ << d_device_name << ": invalid sampling_rate "
+ << sampling_rate << "\n";
+ sampling_rate = 8000;
+ if (ioctl (d_fd, SNDCTL_DSP_SPEED, &sf) < 0){
+ std::cerr << "audio_oss_source: failed to set sampling_rate to 8000\n";
+ throw std::runtime_error ("audio_oss_source");
+ }
+ }
+}
+
+audio_oss_source::~audio_oss_source ()
+{
+ close (d_fd);
+ delete [] d_buffer;
+}
+
+int
+audio_oss_source::work (int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items)
+{
+ float *f0 = (float *) output_items[0];
+ float *f1 = (float *) output_items[1]; // will be invalid if this is mono output
+
+ const int shorts_per_item = 2; // L + R
+ const int bytes_per_item = shorts_per_item * sizeof (short);
+
+ // To minimize latency, never return more than CHUNK_TIME
+ // worth of samples per call to work.
+
+ noutput_items = std::min (noutput_items, d_chunk_size);
+
+ int base = 0;
+ int ntogo = noutput_items;
+
+ while (ntogo > 0){
+ int nbytes = std::min (ntogo, d_chunk_size) * bytes_per_item;
+ int result_nbytes = read (d_fd, d_buffer, nbytes);
+
+ if (result_nbytes < 0){
+ perror ("audio_oss_source");
+ return -1; // say we're done
+ }
+
+ if ((result_nbytes & (bytes_per_item - 1)) != 0){
+ fprintf (stderr, "audio_oss_source: internal error.\n");
+ throw std::runtime_error ("internal error");
+ }
+
+ int result_nitems = result_nbytes / bytes_per_item;
+
+ // now unpack samples into output streams
+
+ switch (output_items.size ()){
+ case 1: // mono output
+ for (int i = 0; i < result_nitems; i++){
+ f0[base+i] = d_buffer[2*i+0] * (1.0 / 32767);
+ }
+ break;
+
+ case 2: // stereo output
+ for (int i = 0; i < result_nitems; i++){
+ f0[base+i] = d_buffer[2*i+0] * (1.0 / 32767);
+ f1[base+i] = d_buffer[2*i+1] * (1.0 / 32767);
+ }
+ break;
+
+ default:
+ assert (0);
+ }
+
+ ntogo -= result_nitems;
+ base += result_nitems;
+ }
+
+ return noutput_items - ntogo;
+}
diff --git a/gr-audio/lib/oss/audio_oss_source.h b/gr-audio/lib/oss/audio_oss_source.h
new file mode 100644
index 000000000..b20ef5c05
--- /dev/null
+++ b/gr-audio/lib/oss/audio_oss_source.h
@@ -0,0 +1,58 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2004-2011 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef INCLUDED_AUDIO_OSS_SOURCE_H
+#define INCLUDED_AUDIO_OSS_SOURCE_H
+
+#include <gr_audio_source.h>
+#include <string>
+
+/*!
+ * \brief audio source using OSS
+ *
+ * Output signature is one or two streams of floats.
+ * Output samples will be in the range [-1,1].
+ */
+
+class audio_oss_source : public audio_source {
+
+ int d_sampling_rate;
+ std::string d_device_name;
+ int d_fd;
+ short *d_buffer;
+ int d_chunk_size;
+
+public:
+ audio_oss_source (int sampling_rate,
+ const std::string device_name = "",
+ bool ok_to_block = true);
+
+ ~audio_oss_source ();
+
+ int work (int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items);
+};
+
+
+
+#endif /* INCLUDED_AUDIO_OSS_SOURCE_H */
diff --git a/gr-audio/lib/oss/gr-audio-oss.conf b/gr-audio/lib/oss/gr-audio-oss.conf
new file mode 100644
index 000000000..6ea14d67e
--- /dev/null
+++ b/gr-audio/lib/oss/gr-audio-oss.conf
@@ -0,0 +1,9 @@
+# This file contains system wide configuration data for GNU Radio.
+# You may override any setting on a per-user basis by editing
+# ~/.gnuradio/config.conf
+
+[audio_oss]
+
+default_input_device = /dev/dsp
+default_output_device = /dev/dsp
+latency = 0.005 # in seconds