A crude 1KHz sinewave generator using bash...

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Postby IamPete » Fri Apr 04, 2014 10:39 pm


By the looks of it /dev/dsp can do other sample rates.
Found this:

Code: Select all
 * parrot.c
 * From "Linux Multimedia Guide"
 * by Jeff Tranter
 * Program to illustrate /dev/dsp device
 * Records several seconds of sound, then echoes it back.
 * Runs until Control-C is pressed.

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/soundcard.h>

#define LENGTH 3      /* how many seconds of speech to store */
#define RATE 44100      /* the sampling rate */
#define SIZE 8         /* sample size: 8 or 16 bits */
#define CHANNELS 1      /* 1 = mono 2 = stereo */

/* this buffer holds the digitized audio */
unsigned char buf[LENGTH * RATE * SIZE * CHANNELS / 8];

main ()
  int fd;         /* sound device file descriptor */
  int arg;         /* argument for ioctl calls */
  int status;         /* return status of system calls */

  /* open sound device */
  fd = open ("/dev/dsp", O_RDWR);
  if (fd < 0)
      perror ("open of /dev/dsp failed");
      exit (1);

  /* set sampling parameters */
  arg = SIZE;         /* sample size */
  status = ioctl (fd, SOUND_PCM_WRITE_BITS, &arg);
  if (status == -1)
    perror ("SOUND_PCM_WRITE_BITS ioctl failed");
  if (arg != SIZE)
    perror ("unable to set sample size");

  arg = CHANNELS;      /* mono or stereo */
  status = ioctl (fd, SOUND_PCM_WRITE_CHANNELS, &arg);
  if (status == -1)
    perror ("SOUND_PCM_WRITE_CHANNELS ioctl failed");
  if (arg != CHANNELS)
    perror ("unable to set number of channels");

  arg = RATE;         /* sampling rate */
  status = ioctl (fd, SOUND_PCM_WRITE_RATE, &arg);
  if (status == -1)
    perror ("SOUND_PCM_WRITE_WRITE ioctl failed");

  while (1)
    {            /* loop until Control-C */
      printf ("Say something:\n");
      status = read (fd, buf, sizeof (buf));   /* record some sound */
      if (status != sizeof (buf))
   perror ("read wrong number of bytes");
      printf ("You said:\n");
      status = write (fd, buf, sizeof (buf));   /* play it back */
      if (status != sizeof (buf))
   perror ("wrote wrong number of bytes");
      /* wait for playback to complete before recording again */
      status = ioctl (fd, SOUND_PCM_SYNC, 0);
      if (status == -1)
   perror ("SOUND_PCM_SYNC ioctl failed");

Also found this:

Posts: 14
Joined: Thu Apr 03, 2014 11:34 am

Postby Bazza » Sat Apr 05, 2014 8:36 am

Hi IamPete...

(Apologies for typos.)

/dev/dsp and /dev/audio CAN be changed but NOT through pure shell scripting, or any other pure scripting language for that matter, without the command line tools to do it.

It is ages since I tried this but IIRC...
Code: Select all
cat < /dev/dsp > /dev/dsp

...does near exactly the same as the C code through the microphone but at the default device parameters.

There is the matter that not all Linux installs have these two devices anyhow. Debian does NOT, PCLinuxOS does(/did?)...
If not, one still has to install "oss-compat" from the distros repositories, assuming these distros have it.

The OSS system is now considered obsolete and ALSA, is generally the default installed norm. PulseAudio is also around but I don't know if it is or can be a default install on the big gun Linux installs.

However that is a C _script_ that has to be compiled not a _script_ that self runs, unless you know of a TRUE C Interpreter.

You have already pointed out that you will have to jump through hoops using a _low_ level language to access what you want but you will not succeed using a default Linux install and its default shell. On this Mac /dev/dsp and /dev/audio are none existent so I use an already powerful command line tool SOX to manipulate data for the sound system for me. It is also platform _independent_. It DOES have to be installed of course but it works the same for ALL platforms including Windows...

I like your mindset though as it is much the same as mine. ;o)

Probably better to start your own thread and keep us informed of this project as I am sure there are many lurkers out there interested in the end results.

I sure am...

Bazza, G0LCU...

Team AMIGA...
User avatar
LXF regular
Posts: 1484
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby IamPete » Sat Apr 05, 2014 12:19 pm

Bazza wrote:

Probably better to start your own thread and keep us informed of this project....

Shall do, just need to do a bit more work on the project first.
Posts: 14
Joined: Thu Apr 03, 2014 11:34 am


Return to Programming

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests