Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wisecracker

macrumors newbie
Original poster
Sep 24, 2012
18
0
In front of my computer.
Hi folks, preamble...

My 3rd upload to this site, hope you enjoy...

This is proof of concept and so far looking good...

For Python-heads, MacBook Pro 13 inch, OSX 10.7.5, Python 2.6.x minimum and an instllation of pyaudio...
You might have to run:-
python2.6<CR>
From a standard Terminal as that is _where_ pyaudio will install by default.
(This might work on new Retina Macbooks too, but I hold no resposibility for the outcome.)

I am attempting to build a kids level project using the external mono microphone input
as an input for a calibrated DC-20KHz Audio Oscilloscope using Python, Tkinter and pyaudio.

(This DEMO test code is being released here first before it is uploaded to the Python section of:-
http://code.activestate.com/ )

The DEMO test code...

Watch for word wrapping etc...
Code:
# SimpleScope_OSX.py
# A standard text mode Python Audio LF Oscilloscope DEMO for a MacBook Pro 13 inch.
# This is the basis for a sound card AF Oscilloscope in development.
#
# Disclaimer:-
#
# You take this information and use it ENTIRELY at your own risk.
# I hold no responsibility for any errors either in this text or with your electronics capabilities...
#
# The reason I have given this away is/was because I needed an audio input for a simple MacBook Pro
# Python Audio-Oscilloscope I am developing for the MacBook Pro 13 inch model.
#
# BEST VIEWED IN PLAIN TEXT!
#
# Input adaptor for the Apple MacBook Pro 13 inch only.
# (This MAY be good for the MBP Retina Versions too!)
#
# Mac_OS Version, OSX 10.7.5...
#
# Machine vintage:- August 2012.
#
# Common 3.5mm Mic/Ear socket cable for external MONO audio input, (and stereo audio output)...
#
# Plug required, (or similar)...
#
# http://www.ebay.co.uk/itm/3-5mm-Mini-Jack-Right-Angle-90-Degree-Solder-4-Pole-Male-Plug-Connector-/320770305756
#
# Resistor, 2K2, 1/8 W, 10% tolerance...
#
# This is the wiring for the auto-switching of the Mic input to external...
#
# Pins.               Wiring.
# -----    TIP        -------
#  1 ---->  O  <----- Left Audio Output +ve.
#  2 ---->  H  <----- Right Audio Output +ve.
#  3 ---->  H  <----- Mono (Mic) Input, Active.
#  4 ---->  H  <----- Common To All Inputs And Outputs.
#          ---
#         |   |
#         |   |______
#         |__________|=========
#
# For the auto-switching capability a 2K2 resistor must be connected across Pins 3 and 4. [Mono (Mic) Input, Active and Common]...
#
# Test by plugging into the socket and check that the "System Preferences > Sound" switches over to external input and output.
# Be aware that it takes a few seconds to switch over, it is NOT instantaneous...
#
# You now have two audio outputs at low impedance and an analogue audio input at around 2K2 _impedance_.
#
# Do NOT drive the input with more than 100mV AC and do NOT connect to a DC _supply_ either.
#
# Do NOT load the audio outputs with less than 33 Ohm resistors.
#
# Do NOT assume that Common is connected to GND, Ground, of the computer, although it may well be.
#
# I am assuming that if you are capable of doing this that you are also capable of doing the subtle level tests etc...
#
# (C)2012, B.Walker, G0LCU. The Cable assembly modifictaions are Public Domain, but the code is GPL2...
#
# Enjoy finding simple solutions to often very difficult problems... ;o)
#
# Tested on Python Versions 2.5.6, 2.6.7 and 2.7.1 with pyaudio installed.
#
# $VER: SimpleScope_OSX.py_Version_0.00.10_(C)2012_B.Walker_G0LCU.

# The imports required...
import os
import sys
import pyaudio

def main():
	# Set everything as global just for this DEMO; my choice... ;o)
	global ScopeScreen
	global ScopeWindow
	global plot
	global position
	global horiz
	global record
	global data
	global grab
	global n
	global dec_val

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

	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("ScopeScreen.txt","wb+")
		ScopeWindow.write(ScopeScreen)
		ScopeWindow.close()

		# Access the sound system...
		stream=pyaudio.PyAudio().open(format=pyaudio.paInt8,channels=1,rate=48000, input=True,frames_per_buffer=1024)
		all=[]
		for n in range(0,48000/1024*1,1):
			record=stream.read(1024)
			all.append(record)
		stream.close()
		pyaudio.PyAudio().terminate()
		record="".join(all)
		# Convert to unsigned data because paUInt8 does NOT work on a MocBook Pro...
		# "struct.unpack" did not work as predicted so done longhand......
		data=""
		for n in range(0,len(record),1):
			dec_val=ord(record[n])
			if dec_val>=0 and dec_val<=127:
				dec_val=dec_val+128
				data=data+chr(dec_val)
			dec_val=ord(record[n])
			if dec_val>=128 and dec_val<=255:
				dec_val=dec_val-128
				data=data+chr(dec_val)

		# Now plot the graph...
		horiz=0
		ScopeWindow=open("ScopeScreen.txt","rb+")
		while horiz<=63:
			# Convert each part of the record string into decimal.
			grab=ord(data[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 pyaudio for OSX 10.7.5."
		print "Ctrl-C to quit..."

main()
# SimpleScope.py program end.
# Enjoy finding simple solutions to often very difficult problems.
A preliminary Tkinter image of the Scope front end is here, much more needs to be added:-

http://wisecracker.host22.com/public/Scope1.tiff

And a GIF anim of an AMIGA project I did years ago is here:-

http://wisecracker.host22.com/public/SCOPE.GIF

There is no need to make the cable shown in the code as the internal microphone will work for this DEMO...

The DEMO uses ONLY text mode inside a Python Terminal as a proof test.

Read the code for more information...

If you guys like what you see then either reply on here or pass me an email on wisecraker_at_tesco.net...

I am assuming that those who try this out know how to set up the microphone levels, etc, etc...

Assuming this lot works......
The AC component will be 8 bit depth but I don't think I can get the DC component better than 6 bits.

The code is GPL2 but the home made cable modifications are Public Domain...

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

Bazza, G0LCU...

Team AMIGA...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.