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

A Linux DEMO to show how to display a waveform using standard text mode Python. The audio device /dev/dsp is used and must be available. Levels are set using the standard audio mixers.

Just feed a signal of say 300Hz to 3KHz, (normal voice spectrum), using say, the internal mic, and watch the waveform appear inside the X-Y graticule.

This method WILL be used for a program proper and may be uploaded to this site in the future.

Read the code for more information.

This code is Public Domain and you may do with it as you please.

Enjoy finding simple solutions to often very difficult problems.

Python, 119 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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# SimpleScope.py
# A standard text mode Python Audio LF Oscilloscope DEMO.
#
# (This working idea WILL become the basis of a Data-Logger/Transient-Recorder
# being worked on for Linux, Windows, Classic AMIGA and WinUAE using Arduino.)
#
# (Original working idea, (C)2010, B.Walker, G0LCU.)
# Now issued as Public Domian. You may do as you please with this code.
#
# To run type at the Python prompt:-
# >>> execfile('/full/path/to/file/SimpleScope.py')<RETURN/ENTER>
# Where "/full/path/to/file/" is the drawer(/folder/directory) that
# "SimpleScope.py" resides.
#
# For this DEMO /dev/dsp must exist for it to work, if not, then install
# oss-compat from your distro's repository.
# Ensure the sound system is not in use by another application.
#
# Tested on PCLinuxOS 2009, Knoppix 5.1.1, (and Debian 6.0.0 <- WITH
# oss-compat installed).
# "setterm" and "clear" are assumed to be available.
#
# This is the only import required for this working DEMO.
import os

def main():
	# Set everything as global for this DEMO.
	global ScopeScreen
	global ScopeWindow
	global audio
	global plot
	global position
	global horiz
	global record
	global grab

	# Allocate values.
	ScopeScreen="(C)2011, B.Walker, G0LCU."
	ScopeWindow="Simple LF Audio Oscilloscope."
	audio="/dev/dsp"
	plot=0
	position=67
	horiz=0
	record=" "
	grab=255

	# Turn off the cursor.
	os.system("setterm -cursor off")

	while 1:

		# This is the basic Osilloscope graticule window for this DEMO.
		ScopeScreen="+-------+-------+-------+-------+-------+-------+-------+--------+\n"
		ScopeScreen=ScopeScreen+"|       |       |       |       +       |       |       |        |\n"
		ScopeScreen=ScopeScreen+"|       |       |       |       +       |       |       |        |\n"
		ScopeScreen=ScopeScreen+"|       |       |       |       +       |       |       |        |\n"
		ScopeScreen=ScopeScreen+"|       |       |       |       +       |       |       |        |\n"
		ScopeScreen=ScopeScreen+"+-------+-------+-------+-------+-------+-------+-------+--------+\n"
		ScopeScreen=ScopeScreen+"|       |       |       |       +       |       |       |        |\n"
		ScopeScreen=ScopeScreen+"|       |       |       |       +       |       |       |        |\n"
		ScopeScreen=ScopeScreen+"|       |       |       |       +       |       |       |        |\n"
		ScopeScreen=ScopeScreen+"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-++\n"
		ScopeScreen=ScopeScreen+"|       |       |       |       +       |       |       |        |\n"
		ScopeScreen=ScopeScreen+"|       |       |       |       +       |       |       |        |\n"
		ScopeScreen=ScopeScreen+"|       |       |       |       +       |       |       |        |\n"
		ScopeScreen=ScopeScreen+"+-------+-------+-------+-------+-------+-------+-------+--------+\n"
		ScopeScreen=ScopeScreen+"|       |       |       |       +       |       |       |        |\n"
		ScopeScreen=ScopeScreen+"|       |       |       |       +       |       |       |        |\n"
		ScopeScreen=ScopeScreen+"|       |       |       |       +       |       |       |        |\n"
		ScopeScreen=ScopeScreen+"+-------+-------+-------+-------+-------+-------+-------+--------+\n"

		# Save the this graticule window for further writing to.
		ScopeWindow=open("/tmp/ScopeScreen.txt","wb+")
		ScopeWindow.write(ScopeScreen)
		ScopeWindow.close()

		# Grab a string of 64 bytes in size at 8KHz sampling speed.
		# If you have an internal microphone just shout into it and
		# see a 4 bit depth waveform of you voice.
		# Alternatively feed a signal into the external mic input socket
		# and adjust the levels using the I/P and O/P volume control mixers.
		audio=file("/dev/dsp","rb")
		record=audio.read(64)
		audio.close()

		horiz=0
		ScopeWindow=open("/tmp/ScopeScreen.txt","rb+")
		while horiz<=63:
			# Convert each part of the record string into decimal.
			grab=ord(record[horiz])
			# Now convert to 4 bit depth for text mode display.
			plot=int(grab/16)
			# Invert to suit the text display window.
			plot=15-plot
			# Don't allow an error.
			if plot<=0: plot=0
			if plot>=15: plot=15
			# Set up the horizontal position and plot.
			position=68+horiz+plot*67
			ScopeWindow.seek(position)
			ScopeWindow.write("o")
			horiz=horiz+1

		# Now get the whole ScopeWindow with the plotted points......
		ScopeWindow.seek(0)
		ScopeScreen=ScopeWindow.read(1206)
		ScopeWindow.close()
		# ......and print it to the terminal window.
		print os.system("clear"),chr(13),"  ",chr(13),
		print ScopeScreen
		print "Simple Audio Oscilloscope DEMO using /dev/dsp in Linux."
		print "Ctrl-C to quit..."

	# Reset the cursor assuming it gets here... ;o)
	os.system("setterm -cursor on")
main()

# SimpleScope.py program end.
# Enjoy finding simple solutions to often very difficult problems.

2 comments

Dominic Innocent 10 years, 4 months ago  # | flag

Hi I'm a 'grey surfer' new to Python & have had some fun with this running it through 'Dr Python'. The main problem has been that it keeps reprinting the grid screen one below the other so fast I end up with 5 or 6 before I can get it to stop. I have managed to get it print only one screen & then stop, but not to keep the grid & have a continuous plot. Given time I will get there; if my wife doesn't give me too many jobs" Have fun. d0m.

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

Hi Dom...

Glad you are having fun with it... :)

I don't use DrPython so I don't know its quirks. However a couple of helpful pointers to give you a fighting chance...

1) at line 25 add...

import time

2) at line 113 add right underneath the print statement......

time.sleep(1)

......to give a one second delay after the scan so that you can see what is going on.

Also check that line 109 is doing what it is supposed to; that is s screen clear...

I assume you are using Python 2.x.x if not....... Python version 3.x.x print function does NOT give quite the same results... :o(

>>> print(os.system("clear"),chr(13),"  ",chr(13),)

Enjoy finding simple solutions to often very difficult problems... ;o)

Bazza...