Dear readership
It’s a softer blog today, like dark chocolate for dessert. To those who know, and to those who must find out : Our best friend in music has tragically orphaned us on the internet. We wail out loud and beat our chests, but in code. This post is no epitaph, but an insignificant reverberation in the just-created hollow. We disclaim : We write, as always, for non-coders and coders alike.
First, let’s prepare a mis-en-place of modules we’re going to need. PyAudio has long served the Speech Processing community for storing and splitting the wave files. It’s the obvious invited. Second in line is PyQT4, that runs the baseline for all that you can see and prod. (The GUI, yeah, you got it). The sycophants include modules like “time”, “wave”, “threading” and “sys”. Look below to get a complete list. We will go over the functions of each as you read.
Our first job is to define the backend function of playing and stopping songs. You instantiate PyAudio using this :
p = pyaudio.PyAudio()
We then proceed to define a function that loops over a folder, and plays music on its own. All you need is a directory with a set of wave files(extension : .wav).
The function instantiates the pyaudio module. The flag defined above is sorta important, to make sure your flitting between music files, keeping track, and pausing is controlled.
We move over to the classes are defined functions, one after the other. The first is a conventional initialization of the GUI function. Not much syntax, not much theory there. Easy enough.
The initUI function is where the real magic rests.
First, you define 3 buttons: Play, Next and Stop. Use button-name.move( ) to position them on your window-to-appear. Divide the space, it takes a couple tries, but you’ll get around to it.
The ‘Exit’ button lets you abort the program when it misbehaves.
**********************************
def initUI(self):
btn1 = QtGui.QPushButton("Play", self)
btn1.resize(btn1.sizeHint())
btn1.move(50, 30)
btn2 = QtGui.QPushButton("Stop", self)
btn2.resize(btn2.sizeHint())
btn2.move(150, 30)
btn3 = QtGui.QPushButton("Next", self)
btn3.resize(btn3.sizeHint())
btn3.move(250, 30)
btn4 = QtGui.QPushButton("Exit", self)
btn4.resize(btn3.sizeHint())
btn4.move(350, 30)
***************************************
Always handy, we thought. Notice that innocuous “count” there. Let that be, we’ll talk about it next week. As of now, it’s marks a counter that makes sure which wavfile is being played.
Ok.. let’s now get into the actual space between the touch and the heartstring. In his modesty, my co-author calls it the buttonClicked function. I like to keep it that way, for my kinda people. In 2 simple lines : press the “Play”, run the baseline function that plays music in the wavefiles. It is here that the connection between the button on the GUI and your backend communicate and act. Essentially, button clicked.
***********************************************************
btn1.clicked.connect(lambda: self.buttonClicked(count))
btn2.clicked.connect(lambda: self.buttonClicked(count))
btn3.clicked.connect(lambda: self.buttonClicked(count))
btn4.clicked.connect(QtCore.QCoreApplication.instance().quit)
************************************************************
The assignment of flags here is glaringly important. (ask me, I spent the afternoon on it) This is what will let the play_files decide what to play and when. For when you press “Stop”, the flag that is set to 1 will render play_files inactive. The Next button is not much different, you run the playlist function again, just that your count index is now the next file in the playlist.
*********************************************************
def buttonClicked(self, count):
sender = self.sender()
#print sender
if sender.text() == 'Play':
self.statusBar().showMessage('Yipee')
flag = 0
p = pyaudio.PyAudio()
#t = multiprocessing.Process(target=play_file())
#t1.daemon = False
#threads.append(t)
#t.start()
play_file(flag,count)
elif sender.text() == 'Stop':
flag = 1
self.statusBar().showMessage('Stopping :(')
#t2 = threading.Thread(target=play_file(flag))
play_file(flag, count)
elif sender.text() == 'Next':
self.statusBar().showMessage('Yipee')
flag = 0
count = count + 1
p = pyaudio.PyAudio()
#t = multiprocessing.Process(target=play_file())
#t1.daemon = False
#threads.append(t)
#t.start()
play_file(flag,count)
else:
self.statusBar().showMessage(sender.text() + ' was pressed')
stream.stop_stream()
**********************************************************
That’s all folks! All you got to do is put the classes and functions together. Call them one by one and sing along.
The code is made opensource for you to try out and make changes as you wish. Navigate to the scripts folder and hit ./music_player.py!!
No comments:
Post a Comment