-
Notifications
You must be signed in to change notification settings - Fork 135
/
api_winmm_windows.go
174 lines (158 loc) · 4.8 KB
/
api_winmm_windows.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2021 The Oto Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oto
import (
"fmt"
"runtime"
"unsafe"
"golang.org/x/sys/windows"
)
var (
winmm = windows.NewLazySystemDLL("winmm")
)
var (
procWaveOutOpen = winmm.NewProc("waveOutOpen")
procWaveOutClose = winmm.NewProc("waveOutClose")
procWaveOutPrepareHeader = winmm.NewProc("waveOutPrepareHeader")
procWaveOutUnprepareHeader = winmm.NewProc("waveOutUnprepareHeader")
procWaveOutWrite = winmm.NewProc("waveOutWrite")
)
type _WAVEHDR struct {
lpData uintptr
dwBufferLength uint32
dwBytesRecorded uint32
dwUser uintptr
dwFlags uint32
dwLoops uint32
lpNext uintptr
reserved uintptr
}
type _WAVEFORMATEX struct {
wFormatTag uint16
nChannels uint16
nSamplesPerSec uint32
nAvgBytesPerSec uint32
nBlockAlign uint16
wBitsPerSample uint16
cbSize uint16
}
const (
_WAVE_FORMAT_IEEE_FLOAT = 3
_WHDR_INQUEUE = 16
)
type _MMRESULT uint
const (
_MMSYSERR_NOERROR _MMRESULT = 0
_MMSYSERR_ERROR _MMRESULT = 1
_MMSYSERR_BADDEVICEID _MMRESULT = 2
_MMSYSERR_ALLOCATED _MMRESULT = 4
_MMSYSERR_INVALIDHANDLE _MMRESULT = 5
_MMSYSERR_NODRIVER _MMRESULT = 6
_MMSYSERR_NOMEM _MMRESULT = 7
_WAVERR_BADFORMAT _MMRESULT = 32
_WAVERR_STILLPLAYING _MMRESULT = 33
_WAVERR_UNPREPARED _MMRESULT = 34
_WAVERR_SYNC _MMRESULT = 35
)
func (m _MMRESULT) Error() string {
switch m {
case _MMSYSERR_NOERROR:
return "MMSYSERR_NOERROR"
case _MMSYSERR_ERROR:
return "MMSYSERR_ERROR"
case _MMSYSERR_BADDEVICEID:
return "MMSYSERR_BADDEVICEID"
case _MMSYSERR_ALLOCATED:
return "MMSYSERR_ALLOCATED"
case _MMSYSERR_INVALIDHANDLE:
return "MMSYSERR_INVALIDHANDLE"
case _MMSYSERR_NODRIVER:
return "MMSYSERR_NODRIVER"
case _MMSYSERR_NOMEM:
return "MMSYSERR_NOMEM"
case _WAVERR_BADFORMAT:
return "WAVERR_BADFORMAT"
case _WAVERR_STILLPLAYING:
return "WAVERR_STILLPLAYING"
case _WAVERR_UNPREPARED:
return "WAVERR_UNPREPARED"
case _WAVERR_SYNC:
return "WAVERR_SYNC"
}
return fmt.Sprintf("MMRESULT (%d)", m)
}
func waveOutOpen(f *_WAVEFORMATEX, callback uintptr) (uintptr, error) {
const (
waveMapper = 0xffffffff
callbackFunction = 0x30000
)
var w uintptr
var fdwOpen uintptr
if callback != 0 {
fdwOpen |= callbackFunction
}
r, _, e := procWaveOutOpen.Call(uintptr(unsafe.Pointer(&w)), waveMapper, uintptr(unsafe.Pointer(f)),
callback, 0, fdwOpen)
runtime.KeepAlive(f)
if _MMRESULT(r) != _MMSYSERR_NOERROR {
if e != nil && e != windows.ERROR_SUCCESS {
return 0, fmt.Errorf("oto: waveOutOpen failed: %w", e)
}
return 0, fmt.Errorf("oto: waveOutOpen failed: %w", _MMRESULT(r))
}
return w, nil
}
func waveOutClose(hwo uintptr) error {
r, _, e := procWaveOutClose.Call(hwo)
if _MMRESULT(r) != _MMSYSERR_NOERROR {
if e != nil && e != windows.ERROR_SUCCESS {
return fmt.Errorf("oto: waveOutClose failed: %w", e)
}
return fmt.Errorf("oto: waveOutClose failed: %w", _MMRESULT(r))
}
return nil
}
func waveOutPrepareHeader(hwo uintptr, pwh *_WAVEHDR) error {
r, _, e := procWaveOutPrepareHeader.Call(hwo, uintptr(unsafe.Pointer(pwh)), unsafe.Sizeof(_WAVEHDR{}))
runtime.KeepAlive(pwh)
if _MMRESULT(r) != _MMSYSERR_NOERROR {
if e != nil && e != windows.ERROR_SUCCESS {
return fmt.Errorf("oto: waveOutPrepareHeader failed: %w", e)
}
return fmt.Errorf("oto: waveOutPrepareHeader failed: %w", _MMRESULT(r))
}
return nil
}
func waveOutUnprepareHeader(hwo uintptr, pwh *_WAVEHDR) error {
r, _, e := procWaveOutUnprepareHeader.Call(hwo, uintptr(unsafe.Pointer(pwh)), unsafe.Sizeof(_WAVEHDR{}))
runtime.KeepAlive(pwh)
if _MMRESULT(r) != _MMSYSERR_NOERROR {
if e != nil && e != windows.ERROR_SUCCESS {
return fmt.Errorf("oto: waveOutUnprepareHeader failed: %w", e)
}
return fmt.Errorf("oto: waveOutUnprepareHeader failed: %w", _MMRESULT(r))
}
return nil
}
func waveOutWrite(hwo uintptr, pwh *_WAVEHDR) error {
r, _, e := procWaveOutWrite.Call(hwo, uintptr(unsafe.Pointer(pwh)), unsafe.Sizeof(_WAVEHDR{}))
runtime.KeepAlive(pwh)
if _MMRESULT(r) != _MMSYSERR_NOERROR {
if e != nil && e != windows.ERROR_SUCCESS {
return fmt.Errorf("oto: waveOutWrite failed: %w", e)
}
return fmt.Errorf("oto: waveOutWrite failed: %w", _MMRESULT(r))
}
return nil
}