Music in Python

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 5
Reputation: elvenstruggle is an unknown quantity at this point 
Solved Threads: 0
elvenstruggle elvenstruggle is offline Offline
Newbie Poster

Music in Python

 
0
  #1
Dec 24th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 908
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Music in Python

 
1
  #2
Dec 24th, 2008
There is a great module called winsound.
  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
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Music in Python

 
0
  #3
Dec 27th, 2008
You can use module pygame in your program and play MP3 music files without having to create a GUI window:
  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
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,389
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Music in Python

 
0
  #4
Dec 27th, 2008
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
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: elvenstruggle is an unknown quantity at this point 
Solved Threads: 0
elvenstruggle elvenstruggle is offline Offline
Newbie Poster

Re: Music in Python

 
0
  #5
Dec 27th, 2008
Originally Posted by paulthom12345 View Post
There is a great module called winsound.
  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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,389
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Music in Python

 
0
  #6
Dec 28th, 2008
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
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 908
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Music in Python

 
0
  #7
Dec 28th, 2008
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().

  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 )
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Music in Python

 
0
  #8
Dec 28th, 2008
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.
drink her pretty
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC