944,014 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 10825
  • Python RSS
Nov 19th, 2005
1

Sound in Python

Expand Post »
I did find the Python code snippet on PMIDI module here. It works fine and I have made some interesting sounds with it. Is there way to save this as sound file?
Similar Threads
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Nov 20th, 2005
1

Re: Sound in Python

The beauty of the PMIDI module is that you can produce sound, be it music or soundeffects, within your program with a few lines of code. This way you don't have to load a number of wave files (.wav) that are typically between 5k and 250k in size. Here is an example ...
python Syntax (Toggle Plain Text)
  1. # some sound effects using PMIDI
  2. # NewNote(beat,duration,note,octave)
  3.  
  4. from PMIDI import *
  5. from time import sleep
  6.  
  7. def bird():
  8. seq = Sequencer()
  9. song = seq.NewSong()
  10. voice = song.NewVoice(0)
  11. voice.SetInstrumentByName('Bird Tweet')
  12. m = voice.NewMeasure()
  13. m.NewNote(0, 8, 'A', 5)
  14. m.NewNote(8, 8, 'D', 5)
  15. m.NewNote(16, 8, 'A', 5)
  16. seq.Play()
  17. sleep(1.2) # enough seconds to play
  18. seq.Close()
  19.  
  20. def gun():
  21. seq = Sequencer()
  22. song = seq.NewSong()
  23. voice = song.NewVoice()
  24. voice.SetInstrumentByName('Gunshot')
  25. m = voice.NewMeasure()
  26. m.NewNote(0, 12, 'F', 4)
  27. seq.Play()
  28. sleep(0.8) # enough seconds to play
  29. seq.Close()
  30.  
  31. def wood():
  32. seq = Sequencer()
  33. song = seq.NewSong()
  34. voice = song.NewVoice()
  35. voice.SetInstrumentByName('Woodblock')
  36. m = voice.NewMeasure()
  37. m.NewNote(0, 24, 'G', 3)
  38. seq.Play()
  39. sleep(1) # enough seconds to play
  40. seq.Close()
  41.  
  42.  
  43. bird() # bird chirps
  44. sleep(1) # wait a second
  45. gun() # idiot shoots it
  46. sleep(1)
  47. wood() # bird hits ground
Last edited by vegaseat; Aug 12th, 2009 at 5:35 pm. Reason: changed the old php tags
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Running a 3rd party program from Python
Next Thread in Python Forum Timeline: Python and BOO





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC