Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Another Python Freebie... An Audio Sweep Generator.

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
Bazza
LXF regular


Joined: Sat Mar 21, 2009 11:16 am
Posts: 1473
Location: Loughborough

PostPosted: Sat Dec 17, 2011 11:42 am    Post subject: Another Python Freebie... An Audio Sweep Generator. Reply with quote

Hi all...

DM might be interested in this little devil...

A Linux Python 3.x.x code to sweep the audio spectrum from 4KHz down
to 100Hz and back again. (It is effectively a siren sound.)

Written so that anyone can understand how it works...

I will be releasing a Python 2.x.x version to code.activestate.com soon.

Watch out for wordwrapping etc...

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

Code:

# SweepGen3x.py
#
# A DEMO Audio Sweep Generator from 4KHz down to 100Hz and back up again
# using standard Text Mode Python. Another kids level piece of code for a very
# simple, FREE, Test Gear project... Hey, it is not perfect but a simple audio
# sweep generator for FREE, what more could you want!
# This working idea is copyright, (C)2010, B.Walker, G0LCU.
# Initially issued to LXF under the MIT licence.
# Written in such a way that anyone can understand how it works.
#
# Tested on PCLinuxOS 2009 and Debian 6.0.0 using Python 3.1.3 and 3.2.2.
# "/dev/dsp" IS required for this to work; therefore if you haven't got it then
# install "oss-compat" from you distro's repository. Ensure the sound system is
# not already in use.
# It is easily possible to lengthen the higher frequency playing times and VERY
# easily alter the output level and to speed up or slow down the sweep speed.
# I'll let the big guns do that for you...
# IMPORTANT NOTE:- Every EVEN number of bytes is a symmetrical "square" wave BUT
# every ODD number of bytes has preference for the "space" by one byte.
#
# To run this DEMO type at the Python prompt......
#
# >>> exec(open("/full/path/to/SweepGen3x.py").read())<RETURN/ENTER>
#
# ......and away you go.
#
# Note:- NO import[s] required at all, good eh! ;o)

def main():
   # Set all "variables" as globals, my choice... ;o)
   global mark
   global space
   global freq
   global stringlength
   global n
   global sweep

   # Allocate initial values.
   mark=b"\xff"
   space=b"\x00"
   freq=mark+space
   # 8KHz is the default sample speed of the sound system.
   # Therefore this sets the lowest frequency, 8KHz/80=100Hz...
   stringlength=80
   n=0
   sweep=0

   # A simple screen clear and user screen for a default Python window...
   for n in range(0,40,1):
      print("\r\n")
   print("Sweep Generator DEMO from 4KHz down to 100HZ and back again...\n")
   print("This just gives 5 SIREN like sweeps but it is enough for this DEMO...\n")
   print("Copyright, (C)2010, B.Walker, G0LCU.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")

   # Open the audio device, "/dev/dsp" for writing.
   audio=open("/dev/dsp", "wb")

   # Sweep for five times only for this DEMO...
   while 1:
      freq=mark+space
      stringlength=80
      n=0
      
      while sweep<=4:
         # Sweep down from 4KHz to 100Hz.
         audio.write(freq)
         # Add a trailing 0x00 byte.
         freq=freq+space
         # Quit when length of "freq" byte-string is 80 bytes.
         if len(freq)>=stringlength: break
         audio.write(freq)
         # Add a leading 0xff byte.
         freq=mark+freq
         # Quit when length of "freq" byte-string is 80 bytes.
         if len(freq)>=stringlength: break
      while 1:
         # Sweep back up again from 100Hz to 4KHz.
         # Start with an empty byte-string.
         freq=b""
         # Now create a new byte square wave byte-string.
         for n in range(0,int((stringlength)/2),1):
            freq=freq+mark
         for n in range(0,int((stringlength)/2),1):
            freq=freq+space
         audio.write(freq)
         # Create a new byte-string reduced by one byte.
         # This removes one 0xff byte.
         stringlength=stringlength-1
         # Quit when length of "freq" byte-string is 2 bytes.
         if len(freq)<=2: break
         # Start with an empty byte-string.
         freq=b""
         # Now create a new byte-string reduced by one byte.
         for n in range(0,int((stringlength)/2),1):
            freq=freq+mark
         for n in range(0,int(((stringlength)/2)+1),1):
            freq=freq+space
         audio.write(freq)
         # This removes one 0x00 byte.
         stringlength=stringlength-1
         # Quit when length of "freq" byte-string is 2 bytes.
         if len(freq)<=2: break
      sweep=sweep+1
      # Ensure a complete exit from the loop.
      if sweep>=5: break
   # On exit ensure the audio device is closed.
   audio.close()
main()

# End of SweepGen3x.py DEMO...
# Enjoy finding simple solutions to often VERY difficult problems... ;o)


Enjoy banging the metal and getting something useful for your home
electronics workbench...

Bazza, G0LCU...
_________________
73...

Bazza, G0LCU...

Team AMIGA...
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast