python c dll_python call c dll | 学步园

"""

Implementation of Raw Audio Socket server spec in pure Python

http://code.google.com/p/rainforce/wiki/RawAudioSocket

"""importsys#-- CHAPTER 1: CONTINUOUS SOUND PLAYBACK WITH WINDOWS WINMM LIBRARY --## Based on tutorial "Playing Audio in Windows using waveOut Interface"# by David Overtonimportctypesfromctypesimportwintypes# 1. Open Sound Device# --- define necessary data structures from mmsystem.hHWAVEOUT=wintypes.HANDLE

WAVE_FORMAT_PCM=0x1WAVE_MAPPER=-1CALLBACK_NULL=0MMSYSERR_NOERROR=0classWAVEFORMATEX(ctypes.Structure):_fields_=[('wFormatTag',wintypes.WORD),# 0x0001 WAVE_FORMAT_PCM. PCM audio# 0xFFFE The format is specified in the WAVEFORMATEXTENSIBLE.SubFormat# Other values are in mmreg.h('nChannels',wintypes.WORD),('SamplesPerSec',wintypes.DWORD),('AvgBytesPerSec',wintypes.DWORD),# for WAVE_FORMAT_PCM is the product of nSamplesPerSec and nBlockAlign('nBlockAlign',wintypes.WORD),# for WAVE_FORMAT_PCM is the product of nChannels and wBitsPerSample# divided by 8 (bits per byte)('wBitsPerSample',wintypes.WORD),# for WAVE_FORMAT_PCM should be equal to 8 or 16('cbSize',wintypes.WORD)]# extra format information size, should be 0# --- /define# Data must be processes in pieces that are multiple of# nBlockAlign bytes of data at a time. Written and read# data from a device must always start at the beginning# of a block. Playback of PCM data can not be started in# the middle of a sample on a non-block-aligned boundary.hwaveout=HWAVEOUT()wavefx=WAVEFORMATEX(WAVE_FORMAT_PCM,2,# nChannels44100,# SamplesPerSec705600,#AvgBytesPerSec=44100SamplesPerSec*16wBitsPerSample4,# nBlockAlign = 2 nChannels * 16 wBitsPerSample / 8 bits per byte16,# wBitsPerSample0)# Open default wave deviceret=ctypes.windll.winmm.waveOutOpen(ctypes.byref(hwaveout),# buffer to receive a handle identifying# the open waveform-audio output deviceWAVE_MAPPER,# constant to point to default wave devicectypes.byref(wavefx),# identifier for data format sent for device0,# DWORD_PTR dwCallback - callback mechanizm0,# DWORD_PTR dwCallbackInstance - user instance data for callbackCALLBACK_NULL# DWORD fdwOpen - flag for opening the device)ifret!=MMSYSERR_NOERROR:sys.exit('Error opening default waveform audio device (WAVE_MAPPER)')print"Default Wave Audio output device is opened successfully"# 2. Write Audio Blocks to Device# --- define necessary data structuresPVOID=wintypes.HANDLE

WAVERR_BASE=32WAVERR_STILLPLAYING=WAVERR_BASE+1classWAVEHDR(ctypes.Structure):_fields_=[('lpData',wintypes.LPSTR),# pointer to waveform buffer('dwBufferLength',wintypes.DWORD),# in bytes('dwBytesRecorded',wintypes.DWORD),# when used in input('dwUser',wintypes.DWORD),# user data('dwFlags',wintypes.DWORD),('dwLoops',wintypes.DWORD),# times to loop, for output buffers only('lpNext',PVOID),# reserved, struct wavehdr_tag *lpNext('reserved',wintypes.DWORD)]# reserved# The lpData, dwBufferLength, and dwFlags members must be set before calling# the waveInPrepareHeader or waveOutPrepareHeader function. (For either# function, the dwFlags member must be set to zero.)# --- /defineclassAudioWriter(object):def__init__(self,hwaveout):self.hwaveout=hwaveoutself.wavehdr=WAVEHDR()defwrite(self,data):self.wavehdr.dwBufferLength=len(data)self.wavehdr.lpData=data# Prepare block for playbackifctypes.windll.winmm.waveOutPrepareHeader(self.hwaveout,ctypes.byref(self.wavehdr),ctypes.sizeof(self.wavehdr))!=MMSYSERR_NOERROR:sys.exit('Error: waveOutPrepareHeader failed')# Write block, returns immediately unless a synchronous driver is# used (not often)ifctypes.windll.winmm.waveOutWrite(self.hwaveout,ctypes.byref(self.wavehdr),ctypes.sizeof(self.wavehdr))!=MMSYSERR_NOERROR:sys.exit('Error: waveOutWrite failed')# [ ] calculate sleep delay based on sample length# iii [ ] Measure CPU usage spike during wait without delayimporttime

time.sleep(1)# Wait until playback is finishedwhileTrue:# unpreparing the header fails until the block is playedret=ctypes.windll.winmm.waveOutUnprepareHeader(self.hwaveout,ctypes.byref(self.wavehdr),ctypes.sizeof(self.wavehdr))ifret==WAVERR_STILLPLAYING:importtime

time.sleep(1)continueifret!=MMSYSERR_NOERROR:sys.exit('Error: waveOutUnprepareHeader failed with code 0x%x'%ret)break# [ ] it's no good to read all the PCM data into memory at oncedata=open('95672__Corsica_S__frequency_change_approved.raw','rb').read()aw=AudioWriter(hwaveout)aw.write(data)# x. Close Sound Devicectypes.windll.winmm.waveOutClose(hwaveout)print"Default Wave Audio output device is closed"#-- /CHAPTER 1 --

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值