-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchannel_area.go
63 lines (53 loc) · 1.33 KB
/
channel_area.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
/*
* Copyright (c) 2019 Zenichi Amano
*
* This file is part of libsoundio, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
package soundio
/*
#include <soundio/soundio.h>
*/
import "C"
import (
"reflect"
"unsafe"
)
// ChannelArea contain sound data.
type ChannelArea struct {
buffer []byte
step int
bytesPerSample int
}
// fields
// Buffer returns buffer.
func (a *ChannelArea) Buffer() []byte {
return a.buffer
}
func (a *ChannelArea) bufferWithFrame(frame int) []byte {
offset := frame * a.step
return a.buffer[offset : offset+a.bytesPerSample]
}
// Step returns ow many bytes it takes to get from the beginning of one sample to
// the beginning of the next sample.
func (a *ChannelArea) Step() int {
return a.step
}
func newChannelArea(ptr uintptr, format Format, channel int, frameCount int) *ChannelArea {
size := C.sizeof_struct_SoundIoChannelArea
areaPtr := ptr + uintptr(channel*size)
area := (*C.struct_SoundIoChannelArea)(unsafe.Pointer(areaPtr))
areaStep := int(area.step)
frameSize := frameCount * areaStep
sh := &reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(area.ptr)),
Len: frameSize,
Cap: frameSize,
}
buffer := *(*[]byte)(unsafe.Pointer(sh))
return &ChannelArea{
buffer: buffer,
step: areaStep,
bytesPerSample: BytesPerSample(format),
}
}