Welcome, guest | Sign In | My Account | Store | Cart

sinebeep.py

Creating an audio file called...

beep.wav

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

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

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...

Enjoy finding simple solutions to often very difficult problems.

Bazza.

Python, 29 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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, ('sinewave.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 on on the fly.
# sinebeep()
# Use any standard audio player to hear it...
# For example, the generic 'aplay' for Linux ALSA machines.

An 8 second burst derivative of this is/was needed for a project I am working on.

It is a bizarre _hack_ to work around Python 3.x.x and using this _hack_ it now works from Python 1.4.0 to 3.4.1 on various platforms.

Enjoy...

2 comments

Peter Farrell 9 years, 4 months ago  # | flag

That works perfectly! But how? What is "chr()"?

I've used a few packages that require some importing before you can create a wavefile. Great work!

Pete

Barry Walker (author) 9 years, 4 months ago  # | flag

Hello Peter...

That works perfectly! But how? What is "chr()"?

https://docs.python.org/3.3/library/functions.html#chr

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.

Remember 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 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...

I've used a few packages that require some importing before you can create a wavefile. Great work!

Thank you.