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

This IS for the big guns as well as the amatuer...

I am always being told that too many comments inside any code is bad and that professional coders are clever enough to work out what a program does without comments...

So here goes! This snippet of code hits /dev/dsp in Linux and has no comments as to what it does. Make sure that /dev/dsp is available in your Linux install - if not then install oss.compat from your repository.

It needs Python 2.6 minimum, but may well work on a much earlier version

Don't run the code first but have a go at working out what it does and see if you are correct... ;o)

You can email me if you think you are/were correct:- wisecracker_at_tesco.net

Bear in mind I don't think this has ever been done before by hitting the /dev/dsp __directly__.

NOTE:- NO imports are required at all!!!

It runs for about 7 seconds before exiting...

Another BIG PLUS for Linux.

(I have a feeling poeple on here might like this, possibly even vote it up. ;)

Answer now at the bottom of this page...

Enjoy...

Bazza, G0LCU...

Python, 46 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# SomeSound.py

audio=file('/dev/dsp', 'wb')
def main():
	for a in range(0,25,1):
		for b in range(15,112,1):
			for c in range(0,1,1):
				audio.write(chr(127+b)+chr(127+b)+chr(127+b)+chr(127+b)+chr(127-b)+chr(127-b)+chr(127-b)+chr(127-b))
		for b in range(112,15,-1):
			for c in range(0,1,1):
				audio.write(chr(127+b)+chr(127+b)+chr(127+b)+chr(127+b)+chr(127-b)+chr(127-b)+chr(127-b)+chr(127-b))
main()
audio.close()



# The modified code below, that you can experiment with. Be aware of wordwrapping, etc...



# SomeSound.py

audio=file('/dev/dsp', 'wb')
def main():
	# "a" is unimportant!
	for a in range(0,25,1):
		f=15
		g=112
		h=1
		i=0
		j=1
		k=1
		l=112
		m=15
		n=-1
		o=0
		p=1
		q=1
		for b in range(f,g,h):
			for c in range(i,j,k):
				audio.write(chr(127+b)+chr(127+b)+chr(127+b)+chr(127+b)+chr(127-b)+chr(127-b)+chr(127-b)+chr(127-b))
		for d in range(l,m,n):
			for e in range(o,p,q):
				audio.write(chr(127+d)+chr(127+d)+chr(127+d)+chr(127+d)+chr(127-d)+chr(127-d)+chr(127-d)+chr(127-d))
main()
audio.close()

Well not one person emailed me with a guess, right or wrong. I can only assume that from the simplest version above without any comments that no one had guessed correctly. Perhaps from this little piece of DEMO code people will realise that comments are important to let one know at least what is going on.

The above code is the simplest way, (that anyone can understand WITH comments), to generate an AMPLITUDE modulated audio signal; commonly known Tremolo. In the simplest example above a 1KHz, (square wave), carrier is amplitude modulated by a triangle wave at almost 75% modulation depth. The modulation frequency in this case is around 4Hz.

Below it is the same code with the same values as before but using variables instead. As it stands it does exactly the same as the original test code above. By messing with these values and preventing Python error reports slow and fast attack and decay times can be done, sawtooth+ and sawtooth- modulation, sine wave modulation if fact numerous options all from "/dev/dsp". Also the length of the character string changes the carrier frequency within the limits of the 8KHz audio __sampling__ speed.

Linux Python "banging the metal" without any imports...

Cool eh!

Bazza, G0LCU.

Enjoy finding simple solutions to often very difficult problems...

The original code has NOT been revised in any way, although it says it is at "revision 2".

Created by Barry Walker on Thu, 12 Jan 2012 (GPL)
Python recipes (4591)
Barry Walker's recipes (73)

Required Modules

  • (none specified)

Other Information and Tasks

  • Licensed under the GPL 2
  • Viewed 6838 times
  • Revision 2 (updated 9 years ago)