Skip to contentSkip to conversation

Playing a sine wave

DISCUSSION Hrafnkell Eiriksson 6 participants 7 messages May 12, 1999 – May 13, 1999 Sound

lore
Share & tools
lore
#0 lore raw

Hi

Is it possible to use the sequencer stuff in the OSS sound driver to let the sound card play a sine wave of a given frequency? Any pointers to info on how to do that if it is possible?

Signature · 5 lines
//-----------------------//-------------------------------------------------
//  Hrafnkell Eiriksson  //  Student of Computer- and Electrical engineering
//  [email protected]     //  at the Univeristy of Iceland
//                       //  "Blessed are they who go around in circles,
//  Finger for PGP key   //   for they shall be known as Wheels"
#1 lore raw
Quoted 4 lines · Hrafnkell Eiriksson

Hi

Is it possible to use the sequencer stuff in the OSS sound driver to let the sound card play a sine wave of a given frequency?

In theory it's possible to play sine waves using the OPL3 FM synth chip available on most older (ISA) soundcards. However this is very inpractical way.

A much easier way is using /dev/dsp. You only need to construct few cycles of sine wave at given frequency to a buffer. Then just keep writing this buffer to /dev/dsp as many times as you need. In fact it may even be possible that somebody has already written this kind of app (look at ftp://sunsite.unc.edu/pub/Linux/apps/sound).

Best regards,

Hannu
-----
Hannu Savolainen ([email protected])
http://www.opensound.com (Open Sound System (OSS))
http://www.compusonic.fi (Finnish OSS pages)

#2 lore raw
In reply to Hrafnkell Eiriksson
Quoted 11 lines · Hannu Savolainen
Quoted 4 lines · Hrafnkell Eiriksson

Hi

Is it possible to use the sequencer stuff in the OSS sound driver to let the sound card play a sine wave of a given frequency?

In theory it's possible to play sine waves using the OPL3 FM synth chip available on most older (ISA) soundcards. However this is very inpractical way.

Quoted 5 lines

A much easier way is using /dev/dsp. You only need to construct few cycles of sine wave at given frequency to a buffer. Then just keep writing this buffer to /dev/dsp as many times as you need. In fact it may even be possible that somebody has already written this kind of app (look at ftp://sunsite.unc.edu/pub/Linux/apps/sound).

The source for esound (http://www.tux.org/~ricdude/EsounD.html)
includes the code necessary to do this in the file, esd.c. I have reproduced it below for your convenience:

Feel free to ask if you have any questions on the code.

buf is the buffer to store the audio data in,
format determines whether to use 8bit unsigned or 16bit signed data, magl is the magnitude for the left side,
magr is the magnitude for the right side,
freq is the desired frequency in Hertz,
speed is the playback rate of the audio device,
length is the number of samples to be stored in buf, offset is used to get a perfect sine waves, buffer after buffer,
by setting offset to the product of the number of buffers already played and the length of the buffer in samples.

void set_audio_buffer( void *buf, esd_format_t format,
int magl, int magr,
int freq, int speed, int length, long offset )

{
    int i;
    float sample;
    float kf = 2.0 * 3.14 * (float)freq / (float)speed;

    unsigned char *uc_buf = (unsigned char *)buf;
    signed short *ss_buf = (signed short *)buf;
    
    /* printf( "fmt=%d, ml=%d, mr=%d, freq=%d, speed=%d, len=%ld\n",
       format, magl, magr, freq, speed, length ); */

    switch ( format & ESD_MASK_BITS )
    {
    case ESD_BITS8:
	for ( i = 0 ; i < length ; i+=2 ) {
	    sample = sin( (float)(i+offset) * kf );
	    uc_buf[i] = 127 + magl * sample;
	    uc_buf[i+1] = 127 + magr * sample;
	}
	break;
    case ESD_BITS16:	/* assume same endian */
	for ( i = 0 ; i < length ; i+=2 ) {
	    sample = sin( (float)(i+offset) * kf );
	    ss_buf[i] = magl * sample;
	    ss_buf[i+1] = magr * sample;
	}
	break;
    default:
	fprintf( stderr, 
		 "unsupported format for set_audio_buffer: 0x%08x\n", 
		 format );
	exit( 1 );
    }


    return;
}
Quoted 7 lines

Best regards,

Hannu
-----
Hannu Savolainen ([email protected])
http://www.opensound.com (Open Sound System (OSS))
http://www.compusonic.fi (Finnish OSS pages)

Signature · 14 lines
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=+
| Eric B. Mitchell         mailto:[email protected] |
| tel: (301) 809 - 3534    Altair Aerospace Corporation |
| tel: (800) 7 - ALTAIR    4201 Northview Dr. Suite 410 |
| fax: (301) 805 - 8122    Bowie, MD  20716             |
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=+
              ,___
          /"\  / o=\  /"""---==/
         /   \_/  \__/   ---==/ 
         |    //\   || /""TT""/ //\   || ||""\
         |   //  \  ||    ||   //  \  || ||__/
         |  //--=\ |L--/ ||  //--=\ || || "=,
          \      ---==/
           \____---==/
#3 lore raw
In reply to Hrafnkell Eiriksson
Quoted 11 lines
Quoted 2 lines

Is it possible to use the sequencer stuff in the OSS sound driver to let the sound card play a sine wave of a given frequency?

In theory it's possible to play sine waves using the OPL3 FM synth chip available on most older (ISA) soundcards. However this is very inpractical way.

A much easier way is using /dev/dsp. You only need to construct few cycles of sine wave at given frequency to a buffer. Then just keep writing this buffer to /dev/dsp as many times as you need. In fact it may even be possible that somebody has already written this kind of app (look at ftp://sunsite.unc.edu/pub/Linux/apps/sound).

See
http://metalab.unc.edu/pub/Linux/apps/sound/dtmf-dial-0.2.tar.gz

It combines two sine waves and outputs to /dev/dsp. You are welcome to hack on it. Internally it has the function to produce sine waves at any integer frequency and sampling rate.

Itai

#4 lore raw
In reply to Hrafnkell Eiriksson

hm. there is a package called siggen to do such things, dont know which devices it uses though...

-cc

Quoted 13 lines · Hrafnkell Eiriksson

Hi

Is it possible to use the sequencer stuff in the OSS sound driver to let the sound card play a sine wave of a given frequency? Any pointers to info on how to do that if it is possible?

Signature · 5 lines
//-----------------------//-------------------------------------------------
//  Hrafnkell Eiriksson  //  Student of Computer- and Electrical engineering
//  [email protected]     //  at the Univeristy of Iceland
//                       //  "Blessed are they who go around in circles,
//  Finger for PGP key   //   for they shall be known as Wheels"
#5 lore raw
In reply to Hrafnkell Eiriksson
Quoted 5 lines · Hannu Savolainen

A much easier way is using /dev/dsp. You only need to construct few cycles of sine wave at given frequency to a buffer. Then just keep writing this buffer to /dev/dsp as many times as you need. In fact it may even be possible that somebody has already written this kind of app (look at ftp://sunsite.unc.edu/pub/Linux/apps/sound).

Yes, I wrote such a program using /dev/dsp. Look for "wave" on sunsite.unc.edu, probably in the directory mentioned above unless they moved it.

It can do sine, square, triangle, and ramp waveforms, in stereo.

--
Jeff Tranter (mailto:[email protected] http://www.pobox.com/~tranter)
Currently running Linux 2.2.7 Learn more at http://www.linux.org

#6 lore raw
In reply to Hrafnkell Eiriksson
Quoted 5 lines · Hannu Savolainen

A much easier way is using /dev/dsp. You only need to construct few cycles of sine wave at given frequency to a buffer. Then just keep writing this buffer to /dev/dsp as many times as you need. In fact it may even be possible that somebody has already written this kind of app (look at ftp://sunsite.unc.edu/pub/Linux/apps/sound).

What about creating a patch with the sine wave and use the wavetable stuff to play it. Thay way the wavetable could change the playback frequency etc.
(Please note that I hardly know what a wavetable is nor what it can do :)

Signature · 5 lines
//-----------------------//-------------------------------------------------
//  Hrafnkell Eiriksson  //  Student of Computer- and Electrical engineering
//  [email protected]     //  at the Univeristy of Iceland
//                       //  "Blessed are they who go around in circles,
//  Finger for PGP key   //   for they shall be known as Wheels"