-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundmod.py
104 lines (83 loc) · 3.13 KB
/
soundmod.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
## Soundmod, Copyright (c) 2021 TFB
## Use freely under zlib terms, see licence file.
from ctypes import WinDLL, c_buffer
global winmm
winmm=WinDLL('winmm.dll')
global aliasnum
aliasnum=0
class vl :
off=0x00000000
low=0x55555555
mid=0x99999999
high=0xCCCCCCCC
full=0xFFFFFFFF
class sf :
_sync=0
_async=1
_nodefault=2
_memory=4
_loop=8
_nostop=16
_nowait=8192
_fromfile=131072
def buildwavheader(datasize=176400, samplerate=44100, channels=2, bps=16) : # datasize of 176400 = 1 second of 16 bit stereo PCM audio
header=bytes
header=b'RIFF'
header=header+int(datasize+36).to_bytes(4,byteorder='little') # length = data length + 44 header bytes - 8 bytes of first fields
header=header+b'WAVE'
header=header+b'fmt '
header=header+int(16).to_bytes(4,byteorder='little') # 16 bytes for PCM format header
header=header+int(1).to_bytes(2,byteorder='little') # 1 = PCM
header=header+int(channels).to_bytes(2,byteorder='little') # number of channels, 1 = mono, 2 = stereo
header=header+int(samplerate).to_bytes(4,byteorder='little') # samplerate
header=header+int(samplerate*channels*(bps/8)).to_bytes(4,byteorder='little') # byterate = samplerate * number of channels * bits per sample / 8
header=header+int(channels*(bps/8)).to_bytes(2,byteorder='little') # block align = number of channels * bits per sample / 8
header=header+int(bps).to_bytes(2,byteorder='little') # bits per sample
header=header+b'data'
header=header+int(datasize).to_bytes(4,byteorder='little') # data length in bytes
return header
def wavload(filename) :
wavefile=open(filename,'rb',0)
wave=wavefile.read()
wavefile.close()
datasize=int.from_bytes(wave[40:44],byteorder='little')
return wave[0:44], wave[44:datasize+2]
def wavdataload(filename) :
wavefile=open(filename,'rb',0)
wave=wavefile.read()
wavefile.close()
datasize=int.from_bytes(wave[40:44],byteorder='little')
return wave[44:datasize+2]
def wavdatasize(header) :
return int.from_bytes(header[40:44],byteorder='little')
def wavtotalsize(header) :
return int.from_bytes(header[4:8],byteorder='little')
def sendcommand(command) :
global winmm
buf=c_buffer(256)
err=winmm.mciSendStringW(command, buf, 254, 0)
return err
def assignwav(filename) :
global aliasnum
aliasnum=aliasnum+1
alias='soundname'+str(aliasnum)
sendcommand(r'Open '+filename+' alias '+alias)
return alias
def freewav(alias) :
sendcommand('Close '+alias)
return
def playwav(alias) :
sendcommand('Play '+alias+' from 0')
return
def stopwav(alias) :
sendcommand('Stop '+alias)
return
def resumewav(alias) :
sendcommand('Play '+alias)
return
def setvol(volume) :
winmm.waveOutSetVolume(None,volume)
return volume
def closewavs() :
sendcommand('Close all')
return