Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

A crude 1KHz sinewave generator using bash...
Goto page Previous  1, 2
 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
IamPete



Joined: Thu Apr 03, 2014 12:34 pm
Posts: 14

PostPosted: Fri Apr 04, 2014 11:39 pm    Post subject: Reply with quote

Bazza

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

Code:

/*
 * 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];

int
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:

http://search.cpan.org/~sethj/Audio-DSP-0.02/DSP.pm
Back to top
View user's profile Send private message
Bazza
LXF regular


Joined: Sat Mar 21, 2009 11:16 am
Posts: 1477
Location: Loughborough

PostPosted: Sat Apr 05, 2014 9:36 am    Post subject: Reply with quote

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:
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...
_________________
73...

Bazza, G0LCU...

Team AMIGA...
Back to top
View user's profile Send private message
IamPete



Joined: Thu Apr 03, 2014 12:34 pm
Posts: 14

PostPosted: Sat Apr 05, 2014 1:19 pm    Post subject: Reply with quote

Bazza wrote:

Quote:

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.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast