PYQT basic exercise (Google search)

Source: Internet
Author: User

"' ps_multimedia_play_sound1.py

Explore the Pyside Qtmultimedia module to play a specified sound

Modified PyQT code from:

Http://www.diotavelli.net/PyQtWiki/Playing%20a%20sound%20with%20QtMultimedia

Pyside is the official lgpl-licensed version of PyQT

For free pyside Windows installers See:

Http://developer.qt.nokia.com/wiki/PySide_Binaries_Windows

Or

http://www.lfd.uci.edu/~gohlke/pythonlibs/

Tested with Python33 and Pyside112 by Vegaseat 29jul2013

‘‘‘

From Pyqt4.qtcore Import *

From Pyqt4.qtgui Import *

From Pyqt4.qtmultimedia Import *

From math import pi, sin

Import struct

Class Window (Qwidget):

def __init__ (self, parent = None):

Qwidget.__init__ (self, parent)

# setgeometry (X_pos, Y_pos, width, height)

Self.setgeometry (300, 300, 400, 80)

Self.setwindowtitle ("Play a specified sound (2 seconds)")

format = Qaudioformat ()

Format.setchannels (1)

Format.setfrequency (22050)

Format.setsamplesize (16)

Format.setcodec ("AUDIO/PCM")

Format.setbyteorder (Qaudioformat.littleendian)

Format.setsampletype (Qaudioformat.signedint)

Self.output = qaudiooutput (format, self)

self.frequency = 100

Self.volume = 15000

Self.buffer = Qbuffer ()

Self.data = Qbytearray ()

Self.pitchslider = Qslider (qt.horizontal)

Self.pitchSlider.setMaximum (2000)

Self.volumeslider = Qslider (qt.horizontal)

Self.volumeSlider.setMaximum (32767)

Self.volumeSlider.setPageStep (1024)

Self.volumeSlider.setValue (Self.volume)

Self.playbutton = Qpushbutton ("&play")

Self.pitch_label = Qlabel ()

Self.pitchSlider.valueChanged.connect (self.changefrequency)

Self.volumeSlider.valueChanged.connect (Self.changevolume)

Self.playButton.clicked.connect (Self.play)

# Layout Managers ...

Formlayout = Qformlayout ()

Formlayout.addrow ("P&itch:", Self.pitchslider)

Formlayout.addrow ("&volume:", Self.volumeslider)

Buttonlayout = Qvboxlayout ()

Buttonlayout.addwidget (Self.pitch_label)

Buttonlayout.addwidget (Self.playbutton)

Buttonlayout.addstretch ()

Horizontallayout = Qhboxlayout (self)

Horizontallayout.addlayout (Formlayout)

Horizontallayout.addlayout (Buttonlayout)

Self.changefrequency ()

def changefrequency (self, value=0):

Self.frequency = + (value * 2)

s = "pitch = {} Hz". Format (self.frequency)

Self.pitch_label.setText (s)

def play (self):

If self.output.state () = = Qaudio.activestate:

Self.output.stop ()

If Self.buffer.isOpen ():

Self.buffer.close ()

Self.createdata ()

Self.buffer.setData (Self.data)

Self.buffer.open (qiodevice.readonly)

Self.buffer.seek (0)

Self.output.start (Self.buffer)

def changevolume (self, value):

Self.volume = value

def createdata (self):

‘‘‘

Create 2 seconds of data with 22050 samples per second,

Each sample being Bits (2 bytes)

‘‘‘

Self.data.clear ()

for k in range (2 * 22050):

t = k/22050.0

value = Int (Self.volume * sin (2 * pi * self.frequency * t))

Self.data.append (Struct.pack ("

# Test the module

if __name__ = = "__main__":

App = Qapplication ([])

window = window ()

Window.show ()

APP.EXEC_ ()

PYQT basic exercise (Google search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.