943,696 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 5478
  • Python RSS
Dec 24th, 2008
0

Music in Python

Expand Post »
Hey guys, I just want to know if there is any ways to add music to a python program (non-GUI, pure text program), and if there is, how?

Thanks a lot!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elvenstruggle is offline Offline
5 posts
since Dec 2008
Dec 24th, 2008
1

Re: Music in Python

There is a great module called winsound.
python Syntax (Toggle Plain Text)
  1. import winsound
  2. winsound.PlaySound("soundFile.wav",SND_ASYNC)
  3. raw_input()
Just make sure to change the name of the file. FOr more reference check out this page:
http://python.active-venture.com/lib...-winsound.html
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Dec 27th, 2008
0

Re: Music in Python

You can use module pygame in your program and play MP3 music files without having to create a GUI window:
Python Syntax (Toggle Plain Text)
  1. # play a MP3 music file using module pygame
  2. # (does not create a GUI frame in this case)
  3. # pygame is free from: http://www.pygame.org/
  4. # ene
  5.  
  6. import pygame
  7.  
  8. def play_music(music_file):
  9. """
  10. stream music with mixer.music module in blocking manner
  11. this will stream the sound from disk while playing
  12. """
  13. clock = pygame.time.Clock()
  14. try:
  15. pygame.mixer.music.load(music_file)
  16. print "Music file %s loaded!" % music_file
  17. except pygame.error:
  18. print "File %s not found! (%s)" % (music_file, pygame.get_error())
  19. return
  20. pygame.mixer.music.play()
  21. while pygame.mixer.music.get_busy():
  22. # check if playback has finished
  23. clock.tick(30)
  24.  
  25.  
  26. # pick MP3 music file you have ...
  27. # (if not in working folder, use full path)
  28. music_file = "Drumtrack.mp3"
  29.  
  30. # set up the mixer
  31. freq = 44100 # audio CD quality
  32. bitsize = -16 # unsigned 16 bit
  33. channels = 2 # 1 is mono, 2 is stereo
  34. buffer = 2048 # number of samples (experiment to get right sound)
  35. pygame.mixer.init(freq, bitsize, channels, buffer)
  36.  
  37. # optional volume 0 to 1.0
  38. pygame.mixer.music.set_volume(0.8)
  39.  
  40. try:
  41. play_music(music_file)
  42. except KeyboardInterrupt:
  43. # if user hits Ctrl/C then exit
  44. # (works only in console mode)
  45. pygame.mixer.music.fadeout(1000)
  46. pygame.mixer.music.stop()
  47. raise SystemExit
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Dec 27th, 2008
0

Re: Music in Python

You can use PyAudio, or Pymedia (I havent use any but pyaudio is simple by looking with eyes). If that is too light, go for DLLs like Bass. Infact Im working with Bass.DLL and works fine for me, though I'm still learning it!

http://www.un4seen.com/forum/?topic=9271.0
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Dec 27th, 2008
0

Re: Music in Python

There is a great module called winsound.
python Syntax (Toggle Plain Text)
  1. import winsound
  2. winsound.PlaySound("soundFile.wav",SND_ASYNC)
  3. raw_input()
Just make sure to change the name of the file. FOr more reference check out this page:
http://python.active-venture.com/lib...-winsound.html
Thanks, this seems very easy. But can it only play .wav files? Is there a way to play mp3 files? Thanks a lot!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elvenstruggle is offline Offline
5 posts
since Dec 2008
Dec 28th, 2008
0

Re: Music in Python

I have pointed out bass.dll and pymedia. If you choose bass.dll, you will have to face ctypes and for pure pythonic solutio, go for pymedia
Here are some links for pymedia:
http://pymedia.org/
http://pymedia.org/docs/index.html
http://pymedia.org/tut/index.html
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Dec 28th, 2008
0

Re: Music in Python

This is an example from the pyMedia website that will play things for you. Note that you need command line arguments for this, if you want to change this just use things like raw_inputs().

python Syntax (Toggle Plain Text)
  1. #! /bin/env python
  2.  
  3. import sys
  4.  
  5. EMULATE=0
  6.  
  7. def aplayer( name, card, rate, tt ):
  8. import pymedia.muxer as muxer, pymedia.audio.acodec as acodec, pymedia.audio.sound as sound
  9. import time
  10. dm= muxer.Demuxer( str.split( name, '.' )[ -1 ].lower() )
  11. snds= sound.getODevices()
  12. if card not in range( len( snds ) ):
  13. raise 'Cannot play sound to non existent device %d out of %d' % ( card+ 1, len( snds ) )
  14. f= open( name, 'rb' )
  15. snd= resampler= dec= None
  16. s= f.read( 32000 )
  17. t= 0
  18. while len( s ):
  19. frames= dm.parse( s )
  20. if frames:
  21. for fr in frames:
  22. # Assume for now only audio streams
  23.  
  24. if dec== None:
  25. print dm.getInfo(), dm.streams
  26. dec= acodec.Decoder( dm.streams[ fr[ 0 ] ] )
  27.  
  28. r= dec.decode( fr[ 1 ] )
  29. if r and r.data:
  30. if snd== None:
  31. print 'Opening sound with %d channels -> %s' % ( r.channels, snds[ card ][ 'name' ] )
  32. snd= sound.Output( int( r.sample_rate* rate ), r.channels, sound.AFMT_S16_LE, card )
  33. if rate< 1 or rate> 1:
  34. resampler= sound.Resampler( (r.sample_rate,r.channels), (int(r.sample_rate/rate),r.channels) )
  35. print 'Sound resampling %d->%d' % ( r.sample_rate, r.sample_rate/rate )
  36.  
  37. data= r.data
  38. if resampler:
  39. data= resampler.resample( data )
  40. if EMULATE:
  41. # Calc delay we should wait to emulate snd.play()
  42.  
  43. d= len( data )/ float( r.sample_rate* r.channels* 2 )
  44. time.sleep( d )
  45. if int( t+d )!= int( t ):
  46. print 'playing: %d sec\r' % ( t+d ),
  47. t+= d
  48. else:
  49. snd.play( data )
  50. if tt> 0:
  51. if snd and snd.getPosition()> tt:
  52. break
  53.  
  54. s= f.read( 512 )
  55.  
  56. while snd.isPlaying():
  57. time.sleep( .05 )
  58.  
  59. # ----------------------------------------------------------------------------------
  60.  
  61. # Play any compressed audio file with adjustable pitch
  62.  
  63. # http://pymedia.org/
  64.  
  65. if len( sys.argv )< 2 or len( sys.argv )> 5:
  66. print "Usage: aplayer <filename> [ sound_card_index, rate( 0..1- slower, 1..4 faster ) ]"
  67. else:
  68. i= 0
  69. r= 1
  70. t= -1
  71. if len( sys.argv )> 2 :
  72. i= int( sys.argv[ 2 ] )
  73. if len( sys.argv )> 3 :
  74. r= float( sys.argv[ 3 ] )
  75. if len( sys.argv )> 4 :
  76. t= int( sys.argv[ 4 ] )
  77. aplayer( sys.argv[ 1 ], i, r, t )
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Dec 28th, 2008
0

Re: Music in Python

PyMedia is older than the hills and hasn't been updated for a few years!

If you are looking for oldies but goodies there is also:
http://pysonic.sourceforge.net/index.html
It uses the ever so popular FMOD.DLL

In case you missed it, the PyGame module will play MP3 files.
Last edited by Ene Uran; Dec 28th, 2008 at 6:13 pm.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005

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: FTP Problems
Next Thread in Python Forum Timeline: Programmatically Select item in TreeCtrl





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


Follow us on Twitter


© 2011 DaniWeb® LLC