A Function To Create A 1 Second Sinewave WAVE Beep File In Python.

sinebeep.py

Creating an audio WAVE file called...

beep.wav

...that can be played using almost ANY audio player available.

This simple DEMO snippet of code generates a 1 second sinewave WAVE file. It IS saved inside the CURRENT drawer so that you can find it... ;o)

Note that the waveform itself is created in pure text mode, that is from decimal 32, (space), to decimal 126, (~)... This makes it possible to create a lot of arbitrary RAW
waveforms any length inside any suitable text editor to _at_least_ 6 bit depth AND IF the full range of decimal 0 to 127 is used, 7 bit depth.

This works on:-

Classic stock AMIGA A1200, using Python 1.4.0.
WinUAE and E-UAE, AmigaOS 3.0.x using Python 1.4.0 to 2.0.1.
Windows, to at least 7, using Python 2.0.1 to 3.3.2.
Various Linux flavours using Python 2.4.6 to 3.2.2.
Apple OSX 10.7.x and above using Python 2.5.6 to 3.4.1.

The file size is 8044 bytes and _IF_ you need to it can be palyed directly without a player on some Linux flavours that have the /dev/dsp device. It is an 8 bit, unsigned integer,
mono, 8000Hz sampling speed 8000 byte RAW file with the WAVE header added.

It will still work with PulseAudio and OSS using...

cat /full/path/to/beep.wav > /dev/dsp

...but with a momenatry click due to the 44 header bytes; but hey it is a beep alternative...

This took a while to work out how to make it Version AND Platform independent but this is the result...

Enjoy finding simple solutions to often very difficult problems.

# sinebeep.py
#
# This creates a file named beep.wav.
#
# (C)2014, B.Walker, G0LCU.
# Issued under the MIT licence.
#
# Works on:-
# The Classic AMIGA A1200, WinUAE and E-UAE from Python 1.4.0 to 2.0.1.
# Windows Vista and 7 from Python 2.0.1 to 3.3.2.
# Linux flavours from Python 2.4.2 to 3.2.2.
# Apple OSX 10.7.5 and above from Python 2.5.6 to 3.4.1.
#
# _Compile_ a 1 second, 1KHz, mono, sinewave burst, ('beep.wav'), for general use.
# IMPORTANT!!! This WILL be saved inside the CURRENT drawer/folder/directory so be aware!
def sinebeep():
	header=[ 82, 73, 70, 70, 100, 31, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32, 16, 0, 0, 0, 1, 0, 1, 0, 64, 31, 0, 0, 64, 31, 0, 0, 1, 0, 8, 0, 100, 97, 116, 97, 64, 31, 0, 0 ]
	waveform=[ 79, 45, 32, 45, 79, 113, 126, 113 ]
	wavefile=open("beep.wav", "w+")
	for hdr in range(0, 44, 1):
		wavefile.write(chr(header[hdr]))
	for sample in range(0, 1000, 1):
		for wf in range(0, 8, 1):
			wavefile.write(chr(waveform[wf]))
	wavefile.close()
# Uncomment the next line to create it on the fly.
# sinebeep()
# Use any standard audio player to hear it...
# For example, the generic 'aplay' for Linux ALSA machines.

The chr() function is common to ALL versions of Python for HEX character values from 0x00 to 0x7F, decimal 0 to 127 inclusive. It COULD be considered eqivalent to bytes
inside these two limits.

As these two limits are effectively 7 bits in size then creating almost any wave shape is possible withing limits of the HW in use and the 7 bit depth.

This is a hack, so......

......therefore it is easily possible to juggle the header to stay inside 7 bits per byte and create ALMOST any 7 bit waveform, any length, mono or stereo at any
standard sampling speed.

It is NOT practically possible to do 16 bit signed values that satisfies ALL Python versions, BUT, it IS if there are two versions, one to suit pre-version 3.0.0 and the other to suit
post-version 3.0.0...

Bazza...

1 Like

Reminds me of an early C project I wrote myself 21 yrs. ago, which does a pure 440 hz sine wave with one byte samples and low sample rate:

 
a440.c
======
 
#include <stdio.h>
#ifdef BORLAND
#include <stdlib.h>
#endif
#include <math.h>
char hdr[]={'R', 'I', 'F', 'F', 0, 0, 0, 0, 'W', 'A', 'V', 'E', 'f', 'm', 't', 32, 16, 0, 0, 0, 1, 0, 1, 0, 17, '+', 0, 0, 17, '+', 0, 0, 1, 0, 8, 0, 'd', 'a', 't', 'a', 0, 0, 0, 0};
FILE * f ;
main(argc,argv)
int argc;
char**argv;
{
  #define SAMPLES 600L
  #define FREQ 440
  #define SPS 11025
  #define PI 3.1415927
  long i ;
  float w, x ;
  if ( argc < 2 )
  {
    fprintf( stderr, "Usage is '%s filename'\n", argv[0] ) ;
    exit( -1 ) ;
  }
  if ( !( f = fopen( argv[1], "wb" ) ) )
  {
    fprintf( stderr, "Can't open '%s' to write\n", argv[1] ) ;
    exit( -2 ) ;
  }
  w = 2 * PI * FREQ / SPS ;
  i = SAMPLES + 36 ;
  hdr[4] = i % 256 ;
  hdr[5] = ( i / 256 ) % 256 ;
  hdr[6] = ( i / 65536L ) % 256 ;
  hdr[7] = i / 16777216L ;
  i = SAMPLES ;
  hdr[40] = i % 256 ;
  hdr[41] = ( i / 256 ) % 256 ;
  hdr[42] = ( i / 65536L ) % 256 ;
  hdr[43] = i / 16777216L ;
  for( i = 0L ; i < sizeof(hdr) ; i++ )
  {
    putc( hdr, f ) ;
  }
  i = 0 ;
  while ( ++i <= SAMPLES )
  {
    putc( (char)( 128.0 + 127.999 * sin( w * i ) ), f ) ;
  }
  fclose( f ) ;
  exit( 0 ) ;
}
1 Like