【脚本语言系列】关于Python音频处理DirectSound, 你需要知道的事
2017-05-17 23:11
876 查看
如何使用DirectSound
# -*- coding:utf-8 -*- # import pywintypes import struct from win32com import directsound import Tkinter import tkFileDialog WAV_HEADER_SIZE = struct.calcsize('<4sl4s4slhhllhh4sl') class Window: def __init__(self): self.root = root = Tkinter.Tk() buttonAdd = Tkinter.Button(root, text = 'Add', command = self.add) buttonAdd.pack(side = 'left') buttonPlay = Tkinter.Button(root, text = 'Play', command = self.play) buttonPlay.pack(side = 'left') buttonStop = Tkinter.Button(root, text = 'Stop', command = self.stop) buttonStop.pack(side = 'left') def add(self): self.file = tkFileDialog.askopenfilename(title = 'Python DirectSound',filetypes =[('WAV','*.wav')]) def play(self): f = open(self.file, 'rb') header = f.read(WAV_HEADER_SIZE) (riff, riffsize, wave, fmt, fmtsize, format, nchannels, samplespersecond, datarate, blockalign, bitspersample, data, size) = \ struct.unpack('<4sl4s4slhhllhh4sl', header) if riff!= 'RIFF' or fmtsize != 16 or fmt!='fmt ' or data != 'data': raise 'Data Error' wfx = pywintypes.WAVEFORMATEX() wfx.wFormatTag = format wfx.nChannels = nchannels wfx.nSamplesPerSec = samplespersecond wfx.nAvgBytesPerSec = datarate wfx.nBlockAlign = blockalign wfx.wBitsPerSample= bitspersample d = directsound.DirectSoundCreate(None, None) d.SetCooperativeLevel(None, directsound.DSSCL_PRIORITY) sdesc = directsound.DSBUFFERDESC() sdesc.dwFlags = ( directsound.DSBCAPS_STICKYFOCUS | directsound.DSBCAPS_CTRLPOSITIONNOTIFY ) sdesc.dwBufferBytes = size sdesc.lpwfxFormat = wfx self.buffer = buffer = d.CreateSoundBuffer(sdesc,None) buffer.Update(0, f.read(size)) buffer.Play(0) def stop(self): self.buffer.Stop() def MainLoop(self): self.root.mainloop() window = Window() window.MainLoop()
1. 界面输出
2. 文件载入
什么是DirectSound
DirectSound是DirectX API的音频(waveaudio)的组件之一,其提供了快速的混音、硬件加速功能,并且可以直接访问相关设备。
Directsound允许进行波形声音的捕获、重放,
也可以通过控制硬件和相应的驱动来活动更多的服务。
相关文章推荐
- 【脚本语言系列】关于Python音频处理WMPlayerOCX,你需要知道的事情
- 【脚本语言系列】关于PythonNoSQL数据库处理memcached,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理csv,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理Avro,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理配置文件.ini,你需要知道的事
- 【脚本语言系列】关于Python异常处理,你需要知道的事
- 【脚本语言系列】关于Python结构化二进制文件处理Microsoft Excel,你需要知道的事
- 【脚本语言系列】关于Python结构化二进制文件处理HDF5,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理MsgPack,你需要知道的事
- 【脚本语言系列】关于 Python 事件处理,你需要知道的事
- 【脚本语言系列】关于Python数据库处理SQLite数据库,你需要知道的事
- 【脚本语言系列】关于Python数据库处理SQLAlchemy,你需要知道的事
- 【脚本语言系列】关于Python基础知识处理字符串,你需要知道的事
- 【脚本语言系列】关于 Python XML处理,你需要知道的事
- 【脚本语言系列】关于Python数据库处理SQLite,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理YAML,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理HTML,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理xml,你需要知道的事
- 【脚本语言系列】关于Python图像处理scikits-image, 需要知道的事
- 【脚本语言系列】关于 Python 错误处理,你需要知道的事