Python, Platform Independent, Pure Audio Sinewave Generator...


 
Thread Tools Search this Thread
Top Forums Programming Python, Platform Independent, Pure Audio Sinewave Generator...
# 1  
Old 01-14-2013
Python, Platform Independent, Pure Audio Sinewave Generator...

IKHz_SW_OSX.py

A DEMO mono _pure_ sinewave generator using standard text mode Python 2.6.7 to at least 2.7.3.

This code is EASILY modifyable to Python version 3.x.x...

This DEMO kids level 1KHz generator is mainly for a MacBook Pro, (13 inch in my case), OSX 10.7.5 and above. See below...

It is a simple piece of testgear for the young amateur electronics enthusiast and uses pyaudio fully installed for it to work.

PyAudio pointer can be obtained from inside the comments in the code...

Macbook Pro hardware earphone plug modifications pointer inside the code too...

(I am not sure if the moderators will delete those lines from the code so search the WWW instead if they are...)

This was primarily for a MacBook Pro 13 inch, but works on at least 2 Linux flavours and Windows Vista 32 bit...

The sinewave generated is near excellent...

Enjoy finding simple solutions to often very difficult problems... Bazza, G0LCU...

(This was uploaded elsewhere and has had a lot of hits.)

Issued as GPL3...

Code:
# 1KHz_SW_OSX.py
#
# A mono _pure_ sinewave generator using STANDARD text mode Python 2.6.7 to at least 2.7.3.
# This DEMO kids level 1KHz generator is mainly for a MacBook Pro, (13 inch in my case), OSX 10.7.5 and above.
# It is another simple piece of testgear for the young amateur electronics enthusiast and
# uses pyaudio fully installed for it to work. Enjoy... ;o)
# PyAudio can be obtained from here:- http://people.csail.mit.edu/hubert/pyaudio/
#
# It also works on Windows Vista, 32 bit, on various machines with Python 2.6.x to 2.7.3.
# It also works on Debian 6.0.0 using Python 2.6.6 on an HP dv2036ea notebook.
# It also works on "Ubuntu 12.04, Python 2.7, Dell built-in soundcard", with many thanks to Hubert Pham, author
# of pyaudio itself, for testing...
#
# The hardware modifictions can be found here:-
# http://code.activestate.com/recipes/578282-for-macbook_pro-heads-only-simple-lf-audio-oscillo/?in=lang-python
#
# Ensure the sound is enabled and the volume is turned up. Use the volume control to vary the amplitude...
#
# Copy the file to a folder/drawer/directory of your choice as "1KHz_SW_OSX.py" without the quotes.
#
# Start the Python interpreter from a Terminal/CLI window.
#
# To run the sinewave generator, (depending upon the platform), just use at the ">>>" prompt:-
#
# >>> execfile("/full/path/to/1KHz_SW_OSX.py")<CR>
#
# And away you go...
#
# This code is issued as GPL3...
#
# Connect an oscilloscope to the earphone socket(s) to see the sinewave waveform(s) being generated.
#
# $VER: 1KHz_SW_OSX.py_Version_0.00.10_(C)2012_B.Walker_G0LCU.

# The only import required...
import pyaudio

# Initialise the only _variable_ in use...
n=0

# Set up a basic user screen...
# This assumes the minimum default 80x24 Terminal window size...
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n$VER: 1KHz_SW_OSX.py_Version_0.00.10_(C)2012_B.Walker_G0LCU.\n")
print("A DEMO kids level, platform independent, 1KHz _pure_ sinewave generator.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")

# Open the stream required, mono mode only...
stream=pyaudio.PyAudio().open(format=pyaudio.paInt8,channels=1,rate=16000,output=True)

# Now generate the 1KHz signal at the speakers/headphone output for about 10 seconds...
# Sine wave, to 8 bit depth only...
for n in range(0,10000,1): stream.write("\x00\x30\x5a\x76\x7f\x76\x5a\x30\x00\xd0\xa6\x8a\x80\x8a\xa6\xd0")

# Close the open _channel(s)_...
stream.close()
pyaudio.PyAudio().terminate()

# End of 1KHz_SW_OSX.py program...
# Enjoy finding simple solutions to often very difficult problems... ;o)

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. OS X (Apple)

A simple variable frequency sinewave audio generator.

Hi all... Well I have not been inactive but working out how to make OSX 10.14.x command line audio player have a variable sample rate. This is a back door as afplay does not have a sample rate flag unlike aplay for ALSA, in Linux flavours. This is a DEMO only but a derivative of it will... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. OS X (Apple)

Variable frequency audio generator...

Hi all... I intend to do an Audio Function Generator using Awk, (already started thanks to Don), but the biggest thing I have struggled with was variable frequency. I was going to generate differing sized waveforms on the fly but that would that would mean the frequencies are dependent on any... (2 Replies)
Discussion started by: wisecracker
2 Replies

3. OS X (Apple)

An Audio Function Generator...

Ok guys, gals and geeks... As from today I am starting to learn awk in earnest doing something totally different. I am going to create a pseudo-Audio_Function Generator centred around OSX 10.11.x minimum. The code below is a tester to see what the possibilities are. All waveforms will be... (11 Replies)
Discussion started by: wisecracker
11 Replies

4. Programming

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... (1 Reply)
Discussion started by: wisecracker
1 Replies

5. OS X (Apple)

A Bash Audio Sweep Generator...

This is a small program as a tester for a basic sweep generator for bandwidth testing of AudioScope.sh. This DEMO is only capable of 4KHz down to about 85Hz and back due to the low bit rate, but it is proof of concept for a much wider variant using a much higher bit rate. The file generated... (4 Replies)
Discussion started by: wisecracker
4 Replies

6. Windows & DOS: Issues & Discussions

A SOX 1KHz Sinewave Generator Using A Batch File...

Hi all... I don't think this has been done before but I am open to being corrected... This batch file generates a 65536 byte binary file to give 8 seconds of pure sinewave at the earphone/speaker output(s)... It uses ONLY a default Windows 32 bit installation, to Windows 7, except for the... (0 Replies)
Discussion started by: wisecracker
0 Replies

7. Shell Programming and Scripting

A Crude 1KHz Audio Sinewave Generator Demo...

A very simple crude sinewave generator. The file required is generated inside the code, is linear interpolated and requires /dev/audio to work. Ensure you have this device, if not the download oss-compat from your OS's repository... It lasts for about 8 seconds before exiting and saves a... (5 Replies)
Discussion started by: wisecracker
5 Replies

8. What is on Your Mind?

When Unix will have a singe version, platform independent OS

I wonder when UNIX will be a OS, will have a single version. (1 Reply)
Discussion started by: abhishek0216
1 Replies
Login or Register to Ask a Question