G
Guest
Hi all...
I am new to this list so treat me gently... ;o)
I use Python almost totally differently to the vast majority of people. I like "banging the metal".
As I know of no other way to give my Python code away I thought I`d join here.
I only use STANDARD Python, no extras...
Here are a couple of snippets.
The first generates SINE, SQUARE, TRIANGLE, SAWTOOTH+, SAWTOOTH-, PULSE+
and PULSE- signals at the speakers/earphone socket(s).
The second is a very simple DEMO which I will use in an attempt as an AudioScope using
the external mic socket(s)...
These are Public Domain and I hope they will be of interest to anyone.
WATCH FOR INCORRECT INDEXING AND WORD WRAPPING...
Enjoy finding simple solutions to often very difficult problems.
======================================================================
1) The Function Generator...
# A fun Python program to generate a Sine, Square, Triangle, Sawtooth,
# and Pulse waveforms using STANDARD Python.
# This is for (PC)Linux(OS), (ONLY?), and was done purely for fun.
# (It now works on Debian 6.0.0 and Knoppix 5.1.1!)
#
# Although it is fairly easy to alter the frequency within limits I
# have left it at approximately 1KHz to keep the code size down...
#
# It would be tricky to change the output level using STANDARD Python
# for (PC)Linux(OS) 2009 using this idea to generate waveforms, but it
# is possible within limits.
#
# Original idea copyright, (C)2009, B.Walker, G0LCU.
#
# DONATED TO LXF AS PUBLIC DOMAIN...
#
# It is assumed that /dev/audio exists; if NOT, then install oss-compat
# from the distro`s repository.
#
# Ensure the sound system is not already in use.
#
# Copy the file to the Lib folder/drawer/directory where Python resides.
# For a quick way to run just use at the ">>>" prompt:-
#
# >>> import afg[RETURN/ENTER]
#
# And away we go...
#
# The waveforms generated are unfiltered and therefore not pure,
# but hey, a function generator, for free, without external hardware,
# AND using standard Python, what more do you want...
# Using my notebook about 150mV p-p was generated per channel at the
# earphone socket(s).
#
# This code is Public Domain and you may do with it as you please...
#
# Coded on a(n) HP dual core notebook running PCLinuxOS 2009 and
# Python 2.5.2 for Linux...
#
# You will need an oscilloscope connected to the earphone socket(s)
# to see the resultant waveform(s) generated, or listen to the
# harshness of the sound... ;o)
#
# It is EASILY possible to generate pseudo-random noise also but
# I'll leave that for you to work out...
# Import any modules, ~sys~ is not rquired but added nevertheless.
import sys
import os
# Clear a terminal window ready to run this program.
print os.system("clear"),chr(13)," ",chr(13),
# The program proper...
def main():
# Make all variables global, a quirk of mine...
global sine
global square
global triangle
global sawtoothplus
global sawtoothminus
global pulseplus
global pulseminus
global waveform
global select
global count
# Allocate values to variables.
# Any discrepancy between random soundcards may require small changes
# in the numeric values inside each waveform mode...
# These all oscillate at around 1KHz.
sine=chr(15)+chr(45)+chr(63)+chr(45)+chr(15)+chr(3)+chr(0)+chr(3)
square=chr(63)+chr(63)+chr(63)+chr(63)+chr(0)+chr(0)+chr(0)+chr(0)
triangle=chr(0)+chr(7)+chr(15)+chr(29)+chr(63)+chr(29)+chr(15)+chr(7)
sawtoothplus=chr(63)+chr(39)+chr(26)+chr(18)+chr(12)+chr(8)+chr(4)+chr(0)
sawtoothminus=chr(0)+chr(4)+chr(8)+chr(12)+chr(18)+chr(26)+chr(39)+chr(63)
pulseplus=chr(0)+chr(63)+chr(63)+chr(63)+chr(63)+chr(63)+chr(63)+chr(63)
pulseminus=chr(63)+chr(0)+chr(0)+chr(0)+chr(0)+chr(0)+chr(0)+chr(0)
# This is the INITIAL default waveform, the Square Wave.
waveform=square
select="G0LCU."
count=1
# A continuous loop to change modes as required...
while 1:
# Set up a basic user window.
print os.system("clear"),chr(13)," ",chr(13),
print
print "Simple Function Generator using STANDARD Python 2.5.2"
print "for PCLinuxOS 2009, issued as Public Domain to LXF..."
print
print "Original idea copyright, (C)2009, B.Walker, G0LCU."
print
print "1) Sinewave."
print "2) Squarewave."
print "3) Triangle."
print "4) Positive going sawtooth."
print "5) Negative going sawtooth."
print "6) Positive going pulse."
print "7) Negative going pulse."
print "Q) or q) to quit..."
print
# Enter a number for the mode required.
select=raw_input("Select a number/letter and press RETURN/ENTER:- ")
if select=="": select="2"
if len(select)!=1: break
if select=="Q": break
if select=="q": break
if select=="1": waveform=sine
if select=="2": waveform=square
if select=="3": waveform=triangle
if select=="4": waveform=sawtoothplus
if select=="5": waveform=sawtoothminus
if select=="6": waveform=pulseplus
if select=="7": waveform=pulseminus
# Re-use the variable ~select~ again...
if select<=chr(48): select="Default OR last"
if select>=chr(56): select="Default OR last"
if select=="1": select="Sine wave"
if select=="2": select="Square wave"
if select=="3": select="Triangle wave"
if select=="4": select="Positive going sawtooth"
if select=="5": select="Negative going sawtooth"
if select=="6": select="Positive going pulse"
if select=="7": select="Negative going pulse"
print os.system("clear"),chr(13)," ",chr(13),
print
print select+" audio waveform generation..."
print
# Open up the audio channel(s) to write directly to.
audio=file('/dev/audio', 'wb')
# Make the tone generation time finite in milliseconds...
# A count of 10000 is 10 seconds of tone burst...
count=0
while count<10000:
# Write the waveform to the audio device.
audio.write(waveform)
count=count+1
# Close the audio device when finished.
audio.close()
main()
# End of demo...
======================================================================
2) Record and Playback using STANDARD Python 2.5.2 and above...
# DEMO code for recording a few seconds of sound and playing back the same
# from inside a terminal running Python 2.x in a Linux distro...
#
# This assumes /dev/audio exists, if NOT, then install oss-compat
# from your distro`s repository.
#
# Original idea copyright, (C)2010, B.Walker, G0LCU.
# Issued as Public Domain to LXF.
#
# You may do as you like with this idea but some acknowledgement
# would be appreciated.
#
# Save in the Python/Lib drawer(/folder/directory) as arp.py.
#
# Use "import arp<RETURN/ENTER>" without the quotes to test it.
#
# Tested on PCLinuxOS 2009, Knoppix 5.1.1, (and Debian 6.0.0 <- WITH
# "oss-compat" installed).
#
# Ensure the sound system is not already in use.
# (I think this is Python 3.x compatible too.)
#
# These two imports NOT needed for this quick demo.
# import sys
# import os
def main():
global record
record=""
# Record from my Laptop`s, Notebook`s and Netbook`s mic.
# Note sample rate unknown at the moment, (8KHz?).
# Shout into the internal mic` for test purposes.
audio=file('/dev/audio', 'rb')
record=audio.read(65536)
audio.close()
# Playback from the sound card(s).
audio=file('/dev/audio', 'wb')
audio.write(record)
audio.close()
main()
# End of record/playback DEMO.
--
73...
Bazza, G0LCU...
Team AMIGA...
http://homepages.tesco.net/wisecracker/
http://main.aminet.net/search?readme=wisecracker
http://mikeos.berlios.de/
I am new to this list so treat me gently... ;o)
I use Python almost totally differently to the vast majority of people. I like "banging the metal".
As I know of no other way to give my Python code away I thought I`d join here.
I only use STANDARD Python, no extras...
Here are a couple of snippets.
The first generates SINE, SQUARE, TRIANGLE, SAWTOOTH+, SAWTOOTH-, PULSE+
and PULSE- signals at the speakers/earphone socket(s).
The second is a very simple DEMO which I will use in an attempt as an AudioScope using
the external mic socket(s)...
These are Public Domain and I hope they will be of interest to anyone.
WATCH FOR INCORRECT INDEXING AND WORD WRAPPING...
Enjoy finding simple solutions to often very difficult problems.
======================================================================
1) The Function Generator...
# A fun Python program to generate a Sine, Square, Triangle, Sawtooth,
# and Pulse waveforms using STANDARD Python.
# This is for (PC)Linux(OS), (ONLY?), and was done purely for fun.
# (It now works on Debian 6.0.0 and Knoppix 5.1.1!)
#
# Although it is fairly easy to alter the frequency within limits I
# have left it at approximately 1KHz to keep the code size down...
#
# It would be tricky to change the output level using STANDARD Python
# for (PC)Linux(OS) 2009 using this idea to generate waveforms, but it
# is possible within limits.
#
# Original idea copyright, (C)2009, B.Walker, G0LCU.
#
# DONATED TO LXF AS PUBLIC DOMAIN...
#
# It is assumed that /dev/audio exists; if NOT, then install oss-compat
# from the distro`s repository.
#
# Ensure the sound system is not already in use.
#
# Copy the file to the Lib folder/drawer/directory where Python resides.
# For a quick way to run just use at the ">>>" prompt:-
#
# >>> import afg[RETURN/ENTER]
#
# And away we go...
#
# The waveforms generated are unfiltered and therefore not pure,
# but hey, a function generator, for free, without external hardware,
# AND using standard Python, what more do you want...
# Using my notebook about 150mV p-p was generated per channel at the
# earphone socket(s).
#
# This code is Public Domain and you may do with it as you please...
#
# Coded on a(n) HP dual core notebook running PCLinuxOS 2009 and
# Python 2.5.2 for Linux...
#
# You will need an oscilloscope connected to the earphone socket(s)
# to see the resultant waveform(s) generated, or listen to the
# harshness of the sound... ;o)
#
# It is EASILY possible to generate pseudo-random noise also but
# I'll leave that for you to work out...
# Import any modules, ~sys~ is not rquired but added nevertheless.
import sys
import os
# Clear a terminal window ready to run this program.
print os.system("clear"),chr(13)," ",chr(13),
# The program proper...
def main():
# Make all variables global, a quirk of mine...
global sine
global square
global triangle
global sawtoothplus
global sawtoothminus
global pulseplus
global pulseminus
global waveform
global select
global count
# Allocate values to variables.
# Any discrepancy between random soundcards may require small changes
# in the numeric values inside each waveform mode...
# These all oscillate at around 1KHz.
sine=chr(15)+chr(45)+chr(63)+chr(45)+chr(15)+chr(3)+chr(0)+chr(3)
square=chr(63)+chr(63)+chr(63)+chr(63)+chr(0)+chr(0)+chr(0)+chr(0)
triangle=chr(0)+chr(7)+chr(15)+chr(29)+chr(63)+chr(29)+chr(15)+chr(7)
sawtoothplus=chr(63)+chr(39)+chr(26)+chr(18)+chr(12)+chr(8)+chr(4)+chr(0)
sawtoothminus=chr(0)+chr(4)+chr(8)+chr(12)+chr(18)+chr(26)+chr(39)+chr(63)
pulseplus=chr(0)+chr(63)+chr(63)+chr(63)+chr(63)+chr(63)+chr(63)+chr(63)
pulseminus=chr(63)+chr(0)+chr(0)+chr(0)+chr(0)+chr(0)+chr(0)+chr(0)
# This is the INITIAL default waveform, the Square Wave.
waveform=square
select="G0LCU."
count=1
# A continuous loop to change modes as required...
while 1:
# Set up a basic user window.
print os.system("clear"),chr(13)," ",chr(13),
print "Simple Function Generator using STANDARD Python 2.5.2"
print "for PCLinuxOS 2009, issued as Public Domain to LXF..."
print "Original idea copyright, (C)2009, B.Walker, G0LCU."
print "1) Sinewave."
print "2) Squarewave."
print "3) Triangle."
print "4) Positive going sawtooth."
print "5) Negative going sawtooth."
print "6) Positive going pulse."
print "7) Negative going pulse."
print "Q) or q) to quit..."
# Enter a number for the mode required.
select=raw_input("Select a number/letter and press RETURN/ENTER:- ")
if select=="": select="2"
if len(select)!=1: break
if select=="Q": break
if select=="q": break
if select=="1": waveform=sine
if select=="2": waveform=square
if select=="3": waveform=triangle
if select=="4": waveform=sawtoothplus
if select=="5": waveform=sawtoothminus
if select=="6": waveform=pulseplus
if select=="7": waveform=pulseminus
# Re-use the variable ~select~ again...
if select<=chr(48): select="Default OR last"
if select>=chr(56): select="Default OR last"
if select=="1": select="Sine wave"
if select=="2": select="Square wave"
if select=="3": select="Triangle wave"
if select=="4": select="Positive going sawtooth"
if select=="5": select="Negative going sawtooth"
if select=="6": select="Positive going pulse"
if select=="7": select="Negative going pulse"
print os.system("clear"),chr(13)," ",chr(13),
print select+" audio waveform generation..."
# Open up the audio channel(s) to write directly to.
audio=file('/dev/audio', 'wb')
# Make the tone generation time finite in milliseconds...
# A count of 10000 is 10 seconds of tone burst...
count=0
while count<10000:
# Write the waveform to the audio device.
audio.write(waveform)
count=count+1
# Close the audio device when finished.
audio.close()
main()
# End of demo...
======================================================================
2) Record and Playback using STANDARD Python 2.5.2 and above...
# DEMO code for recording a few seconds of sound and playing back the same
# from inside a terminal running Python 2.x in a Linux distro...
#
# This assumes /dev/audio exists, if NOT, then install oss-compat
# from your distro`s repository.
#
# Original idea copyright, (C)2010, B.Walker, G0LCU.
# Issued as Public Domain to LXF.
#
# You may do as you like with this idea but some acknowledgement
# would be appreciated.
#
# Save in the Python/Lib drawer(/folder/directory) as arp.py.
#
# Use "import arp<RETURN/ENTER>" without the quotes to test it.
#
# Tested on PCLinuxOS 2009, Knoppix 5.1.1, (and Debian 6.0.0 <- WITH
# "oss-compat" installed).
#
# Ensure the sound system is not already in use.
# (I think this is Python 3.x compatible too.)
#
# These two imports NOT needed for this quick demo.
# import sys
# import os
def main():
global record
record=""
# Record from my Laptop`s, Notebook`s and Netbook`s mic.
# Note sample rate unknown at the moment, (8KHz?).
# Shout into the internal mic` for test purposes.
audio=file('/dev/audio', 'rb')
record=audio.read(65536)
audio.close()
# Playback from the sound card(s).
audio=file('/dev/audio', 'wb')
audio.write(record)
audio.close()
main()
# End of record/playback DEMO.
--
73...
Bazza, G0LCU...
Team AMIGA...
http://homepages.tesco.net/wisecracker/
http://main.aminet.net/search?readme=wisecracker
http://mikeos.berlios.de/