Python Forum
Combing codes to play wav file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combing codes to play wav file
#1
Hello all and Happy Holidays

I have embarked on a project that has enticed me into coding for the first time in my life and now I am hooked. Please bare with me I am on my second week of coding, I am watching tutorials left and right learning as fast as I can.


So I have a Raspberry pi with 4x4 button pad(Trellis) that uses i2C. In python 2.7 I have pygame(This was the only wav player I could get to work in Python, if any one know of a better wav player , please let me know). Also in Python I have a GUI that will eventually load the wav files to the corresponding buttons(pads). So I have 3 different codes going on that I need to combine into one code.

Basically I want to press the GUI button and a combo box, combo box with  a dropdown list of wavs, select wav and it will load into say pad 1, I now press pad one on the trellis and it will play the sound.

Here is the GUI
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'Looper808.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
   _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
   def _fromUtf8(s):
       return s

try:
   _encoding = QtGui.QApplication.UnicodeUTF8
   def _translate(context, text, disambig):
       return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
   def _translate(context, text, disambig):
       return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
   def setupUi(self, MainWindow):
       MainWindow.setObjectName(_fromUtf8("MainWindow"))
       MainWindow.resize(800, 600)
       MainWindow.setStyleSheet(_fromUtf8("background-color: rgb(113, 113, 113);"))
       self.centralwidget = QtGui.QWidget(MainWindow)
       self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
       self.Pad1 = QtGui.QPushButton(self.centralwidget)
       self.Pad1.setGeometry(QtCore.QRect(30, 50, 93, 71))
       self.Pad1.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad1.setObjectName(_fromUtf8("Pad1"))
       self.Pad2 = QtGui.QPushButton(self.centralwidget)
       self.Pad2.setGeometry(QtCore.QRect(160, 50, 93, 71))
       self.Pad2.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad2.setObjectName(_fromUtf8("Pad2"))
       self.Pad4 = QtGui.QPushButton(self.centralwidget)
       self.Pad4.setGeometry(QtCore.QRect(420, 50, 93, 71))
       self.Pad4.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad4.setObjectName(_fromUtf8("Pad4"))
       self.Pad3 = QtGui.QPushButton(self.centralwidget)
       self.Pad3.setGeometry(QtCore.QRect(290, 50, 93, 71))
       self.Pad3.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad3.setObjectName(_fromUtf8("Pad3"))
       self.Pad7 = QtGui.QPushButton(self.centralwidget)
       self.Pad7.setGeometry(QtCore.QRect(290, 160, 93, 71))
       self.Pad7.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad7.setObjectName(_fromUtf8("Pad7"))
       self.Pad6 = QtGui.QPushButton(self.centralwidget)
       self.Pad6.setGeometry(QtCore.QRect(160, 160, 93, 71))
       self.Pad6.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad6.setObjectName(_fromUtf8("Pad6"))
       self.Pad8 = QtGui.QPushButton(self.centralwidget)
       self.Pad8.setGeometry(QtCore.QRect(420, 160, 93, 71))
       self.Pad8.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad8.setObjectName(_fromUtf8("Pad8"))
       self.Pad5 = QtGui.QPushButton(self.centralwidget)
       self.Pad5.setGeometry(QtCore.QRect(30, 160, 93, 71))
       self.Pad5.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad5.setObjectName(_fromUtf8("Pad5"))
       self.Pad15 = QtGui.QPushButton(self.centralwidget)
       self.Pad15.setGeometry(QtCore.QRect(290, 380, 93, 71))
       self.Pad15.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad15.setObjectName(_fromUtf8("Pad15"))
       self.Pad11 = QtGui.QPushButton(self.centralwidget)
       self.Pad11.setGeometry(QtCore.QRect(290, 270, 93, 71))
       self.Pad11.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad11.setObjectName(_fromUtf8("Pad11"))
       self.Pad13 = QtGui.QPushButton(self.centralwidget)
       self.Pad13.setGeometry(QtCore.QRect(30, 380, 93, 71))
       self.Pad13.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad13.setObjectName(_fromUtf8("Pad13"))
       self.Pad10 = QtGui.QPushButton(self.centralwidget)
       self.Pad10.setGeometry(QtCore.QRect(160, 270, 93, 71))
       self.Pad10.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad10.setObjectName(_fromUtf8("Pad10"))
       self.Pad12 = QtGui.QPushButton(self.centralwidget)
       self.Pad12.setGeometry(QtCore.QRect(420, 270, 93, 71))
       self.Pad12.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad12.setObjectName(_fromUtf8("Pad12"))
       self.Pad9 = QtGui.QPushButton(self.centralwidget)
       self.Pad9.setGeometry(QtCore.QRect(30, 270, 93, 71))
       self.Pad9.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad9.setObjectName(_fromUtf8("Pad9"))
       self.Pad14 = QtGui.QPushButton(self.centralwidget)
       self.Pad14.setGeometry(QtCore.QRect(160, 380, 93, 71))
       self.Pad14.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad14.setObjectName(_fromUtf8("Pad14"))
       self.Pad16 = QtGui.QPushButton(self.centralwidget)
       self.Pad16.setGeometry(QtCore.QRect(420, 380, 93, 71))
       self.Pad16.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
       self.Pad16.setObjectName(_fromUtf8("Pad16"))
       self.Bank1 = QtGui.QComboBox(self.centralwidget)
       self.Bank1.setGeometry(QtCore.QRect(580, 80, 131, 41))
       self.Bank1.setStyleSheet(_fromUtf8("background-color: rgb(0, 170, 255);"))
       self.Bank1.setObjectName(_fromUtf8("Bank1"))
       self.Bank2 = QtGui.QComboBox(self.centralwidget)
       self.Bank2.setGeometry(QtCore.QRect(580, 170, 131, 41))
       self.Bank2.setStyleSheet(_fromUtf8("background-color: rgb(0, 170, 255);"))
       self.Bank2.setObjectName(_fromUtf8("Bank2"))
       self.Bank3 = QtGui.QComboBox(self.centralwidget)
       self.Bank3.setGeometry(QtCore.QRect(580, 250, 131, 41))
       self.Bank3.setStyleSheet(_fromUtf8("background-color: rgb(0, 170, 255);"))
       self.Bank3.setObjectName(_fromUtf8("Bank3"))
       self.Bank4 = QtGui.QComboBox(self.centralwidget)
       self.Bank4.setGeometry(QtCore.QRect(580, 330, 131, 41))
       self.Bank4.setStyleSheet(_fromUtf8("background-color: rgb(0, 170, 255);"))
       self.Bank4.setObjectName(_fromUtf8("Bank4"))
       self.Bank1_2 = QtGui.QLabel(self.centralwidget)
       self.Bank1_2.setGeometry(QtCore.QRect(610, 45, 71, 21))
       self.Bank1_2.setObjectName(_fromUtf8("Bank1_2"))
       self.Bank2_2 = QtGui.QLabel(self.centralwidget)
       self.Bank2_2.setGeometry(QtCore.QRect(610, 130, 71, 21))
       self.Bank2_2.setObjectName(_fromUtf8("Bank2_2"))
       self.Bank3_2 = QtGui.QLabel(self.centralwidget)
       self.Bank3_2.setGeometry(QtCore.QRect(610, 220, 71, 21))
       self.Bank3_2.setObjectName(_fromUtf8("Bank3_2"))
       self.Bank4_2 = QtGui.QLabel(self.centralwidget)
       self.Bank4_2.setGeometry(QtCore.QRect(610, 300, 71, 21))
       self.Bank4_2.setObjectName(_fromUtf8("Bank4_2"))
       MainWindow.setCentralWidget(self.centralwidget)
       self.menubar = QtGui.QMenuBar(MainWindow)
       self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
       self.menubar.setObjectName(_fromUtf8("menubar"))
       self.menuFile = QtGui.QMenu(self.menubar)
       self.menuFile.setObjectName(_fromUtf8("menuFile"))
       MainWindow.setMenuBar(self.menubar)
       self.statusbar = QtGui.QStatusBar(MainWindow)
       self.statusbar.setObjectName(_fromUtf8("statusbar"))
       MainWindow.setStatusBar(self.statusbar)
       self.actionSave = QtGui.QAction(MainWindow)
       self.actionSave.setObjectName(_fromUtf8("actionSave"))
       self.actionExit = QtGui.QAction(MainWindow)
       self.actionExit.setObjectName(_fromUtf8("actionExit"))
       self.menuFile.addAction(self.actionSave)
       self.menuFile.addSeparator()
       self.menuFile.addAction(self.actionExit)
       self.menubar.addAction(self.menuFile.menuAction())

       self.retranslateUi(MainWindow)
       QtCore.QMetaObject.connectSlotsByName(MainWindow)

   def retranslateUi(self, MainWindow):
       MainWindow.setWindowTitle(_translate("MainWindow", "Zip808", None))
       self.Pad1.setText(_translate("MainWindow", "Pad1", None))
       self.Pad2.setText(_translate("MainWindow", "Pad2", None))
       self.Pad4.setText(_translate("MainWindow", "Pad4", None))
       self.Pad3.setText(_translate("MainWindow", "Pad3", None))
       self.Pad7.setText(_translate("MainWindow", "Pad7", None))
       self.Pad6.setText(_translate("MainWindow", "Pad6", None))
       self.Pad8.setText(_translate("MainWindow", "Pad8", None))
       self.Pad5.setText(_translate("MainWindow", "Pad5", None))
       self.Pad15.setText(_translate("MainWindow", "Pad15", None))
       self.Pad11.setText(_translate("MainWindow", "Pad11", None))
       self.Pad13.setText(_translate("MainWindow", "Pad13", None))
       self.Pad10.setText(_translate("MainWindow", "Pad10", None))
       self.Pad12.setText(_translate("MainWindow", "Pad12", None))
       self.Pad9.setText(_translate("MainWindow", "Pad9", None))
       self.Pad14.setText(_translate("MainWindow", "Pad14", None))
       self.Pad16.setText(_translate("MainWindow", "Pad16", None))
       self.Bank1_2.setText(_translate("MainWindow", "Bank1", None))
       self.Bank2_2.setText(_translate("MainWindow", "Bank1", None))
       self.Bank3_2.setText(_translate("MainWindow", "Bank3", None))
       self.Bank4_2.setText(_translate("MainWindow", "Bank4", None))
       self.menuFile.setTitle(_translate("MainWindow", "File", None))
       self.actionSave.setText(_translate("MainWindow", "Save", None))
       self.actionExit.setText(_translate("MainWindow", "Exit", None))


if __name__ == "__main__":
   import sys
   app = QtGui.QApplication(sys.argv)
   MainWindow = QtGui.QMainWindow()
   ui = Ui_MainWindow()
   ui.setupUi(MainWindow)
   MainWindow.show()
   sys.exit(app.exec_())
This the Trellis
import time
import Adafruit_Trellis

# This example shows reading buttons and setting/clearing buttons in a loop
#   "momentary" mode has the LED light up only when a button is pressed
#   "latching" mode lets you turn the LED on/off when pressed
#
#   Up to 8 matrices per I2C bus can be used but this example will show 4 or 1

MOMENTARY = 0
LATCHING = 1
# Set the mode here:
MODE = MOMENTARY

matrix0 = Adafruit_Trellis.Adafruit_Trellis()

# uncomment the below to add 3 more matrices
#
# matrix1 = Adafruit_Trellis()
# matrix2 = Adafruit_Trellis()
# matrix3 = Adafruit_Trellis()
# you can add more matrices (up to 8 per I2C bus)

# Just one
trellis = Adafruit_Trellis.Adafruit_TrellisSet(matrix0)
# or use the below to select 4, up to 8 per I2C bus can be passed in
# trellis = Adafruit_Trellis.Adafruit_TrellisSet(matrix0, matrix1, matrix2, matrix3)

# set to however many you're working with here, up to 8 per I2C bus
NUMTRELLIS = 1

numKeys = NUMTRELLIS * 16


# Connect Trellis Vin to 5V and Ground to ground.
# Connect Trellis INT wire to a digital input (optional)
# Connect Trellis I2C SDA pin to your board's SDA line
# Connect Trellis I2C SCL pin to your board's SCL line
# All Trellises on an I2C bus share the SDA, SCL and INT pin! 
# Even 8 tiles use only 3 wires max

# Set this to the number of the I2C bus that the Trellises are attached to:
I2C_BUS = 1

# Setup
print 'Trellis Demo'

# TODO: Setup the INT input

# begin() with the I2C addresses and bus numbers of each panel in order
# I find it easiest if the addresses are in order
trellis.begin((0x70, I2C_BUS))   # only one
# trellis.begin((0x70,  I2C_BUS), (0x71, I2C_BUS), (0x72, I2C_BUS), (0x73, I2C_BUS))  # or four!

# light up all the LEDs in order
for i in range(numKeys):
trellis.setLED(i)
trellis.writeDisplay()
time.sleep(0.05)
# then turn them off
for i in range(numKeys):
trellis.clrLED(i)
trellis.writeDisplay()
time.sleep(0.05)

# Loop
print 'Press Ctrl-C to quit.'
while True:
time.sleep(0.03)

if MODE == MOMENTARY:
# If a button was just pressed or released...
if trellis.readSwitches():
# go through every button
for i in range(numKeys):
# if it was pressed, turn it on
if trellis.justPressed(i):
print 'v{0}'.format(i)
trellis.setLED(i)
# if it was released, turn it off
if trellis.justReleased(i):
print '^{0}'.format(i)
trellis.clrLED(i)
# tell the trellis to set the LEDs we requested
trellis.writeDisplay()

if MODE == LATCHING:
# If a button was just pressed or released...
if trellis.readSwitches():
# go through every button
for i in range(numKeys):
# if it was pressed...
if trellis.justPressed(i):
print 'v{0}'.format(i)
# Alternate the LED
if trellis.isLED(i):
trellis.clrLED(i)
else:
trellis.setLED(i)
# tell the trellis to set the LEDs we requested
trellis.writeDisplay()
and the pygame wav player
import pygame

#Initialise pygame and the mixer
pygame.init()
pygame.mixer.init()

#load the sound file
mysound = pygame.mixer.Sound("sitar.wav")

#play the sound file for 10 seconds and then stop it
mysound.play()
time.sleep(10)
mysound.stop()
Soooo if any one can point me in the right direction that would be great, I am still not done with the GUI , its still needs attention. I am also still diving into the tutorials and maybe even find the answers myself but if anyone wants to help that is great, this an open source project that I will be uploading to github as soon as I figure out github.

I also know this would be easier if I was just working on one button getting it to load and play just the one sound  but the trellis is what throws me off, its a button matrix but in the code is referred to as trellis and well is just plain different from a simple button

Again thanx for any info
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I send a .mp3 to play through my mic? ejected 20 20,287 Sep-01-2022, 02:38 AM
Last Post: victormayer
  Play the next music in a list Pymax 0 1,219 Jul-28-2021, 07:27 PM
Last Post: Pymax
  how do i play an mp3 from a list gr3yali3n 3 2,151 Dec-01-2020, 08:50 AM
Last Post: Axel_Erfurt
  How to play against the computer Help_me_Please 4 4,085 Aug-29-2019, 03:37 PM
Last Post: ThomasL
  Play Audio File w/ Python ducky_12 8 4,637 Sep-30-2018, 08:54 AM
Last Post: wavic
  SoX Sound Exchange sample code to Rec & Play audio file Fran_3 3 6,569 Sep-11-2017, 09:13 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020