1

I looked at this question: pyaudio help play a file

While this question did get answered I never got a clear answer of where to actually put the song file.


This is the code for playing a WAVE file:

""" Play a WAVE file. """

import pyaudio
import wave
import sys

chunk = 1024

if len(sys.argv) < 2:
    print "Plays a wave file.\n\n" +\
          "Usage: %s filename.wav" % sys.argv[0]
    sys.exit(-1)

wf = wave.open(sys.argv[1], 'rb')

p = pyaudio.PyAudio()

# open stream
stream = p.open(format =
                p.get_format_from_width(wf.getsampwidth()),
                channels = wf.getnchannels(),
                rate = wf.getframerate(),
                output = True)

# read data
data = wf.readframes(chunk)

# play stream
while data != '':
    stream.write(data)
    data = wf.readframes(chunk)

stream.close()
p.terminate()

I've looked through the code but I can't find anything in the code where I actually insert the music file itself. When I press the "Play" button in my program (I use wxform with this program) nothing is played.

Community
  • 1
  • 1
user1197211
  • 31
  • 1
  • 3

4 Answers4

1

Just know a few things of python, pyaudio but it seems that the song file is the first argument that is passed to the program when you execute it.

Just change insert an argument like this :

 python your-python_file.py sound_file.wav

regards.

1

I don't know anything of pyaudio but it seems pretty clear that the song file is the first argument that is passed to the program when you execute it. Look att this line: wf = wave.open(sys.argv[1], 'rb') Just change to sys.arg[1] to 'c:/filename.wav' or something.

And the program won't run as it is written now if you don't pass any argument to it. Because of the if len(sys.argv) < 2 block

Niclas Nilsson
  • 5,691
  • 3
  • 30
  • 43
  • And also because `sys.argv[1]` would raise an exception. – Niklas B. Feb 08 '12 at 13:27
  • Yeah. But that reflection was based on the thought that he change th `wf =` line and then still got no audio playing... But of course, that would show up in the output. – Niclas Nilsson Feb 08 '12 at 13:28
  • Ah, yes, didn't consider that. I guess I just figured he'd be smart enough to remove that `if` statement, but I'm not so sure... – Niklas B. Feb 08 '12 at 21:37
1

The magic line is:

wf = wave.open(sys.argv[1], 'rb')

This seems to say that the first argument to the script (sys.argv[1]) is used as the input for waves.

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
0

Here is a solution : Comment the If Statment and directly add the file name to play

import pyaudio
import wave
import sys

CHUNK = 1024

#if len(sys.argv) < 2:
#    print("Plays a wave file.\n\nUsage: %s output.wav" % sys.argv[0])
#    sys.exit(-1)

wf = wave.open("output.wav", 'rb')

p = pyaudio.PyAudio()

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True)

data = wf.readframes(CHUNK)

while data != '':
    stream.write(data)
    data = wf.readframes(CHUNK)

stream.stop_stream()
stream.close()

p.terminate()
Mr T
  • 1,409
  • 1
  • 17
  • 24