This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat_bit8.go
More file actions
112 lines (91 loc) · 2.78 KB
/
format_bit8.go
File metadata and controls
112 lines (91 loc) · 2.78 KB
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
package sampling
import (
"encoding/binary"
"io"
"github.com/gotracker/gomixing/volume"
)
const (
cSample8BitDataCoeff = 0x80
cSample8BitVolumeCoeff = volume.Volume(1) / cSample8BitDataCoeff
cSample8BitBytes = 1
)
// Sample8BitSigned is a signed 8-bit sample
type Sample8BitSigned struct{}
// toVolume returns the volume value for the sample
func (Sample8BitSigned) toVolume(v int8) volume.Volume {
return volume.Volume(v) * cSample8BitVolumeCoeff
}
// fromVolume returns the volume value for the sample
func (Sample8BitSigned) fromVolume(v volume.Volume) int8 {
return int8(v.ToIntSample(8))
}
// Size returns the size of the sample in bytes
func (Sample8BitSigned) Size() int {
return cSample8BitBytes
}
// ReadAt reads a value from the reader provided in the byte order provided
func (s Sample8BitSigned) ReadAt(data []byte, ofs int64) (volume.Volume, error) {
if len(data) <= int(ofs) {
return 0, io.EOF
}
if ofs < 0 {
ofs = 0
}
v := int8(data[ofs])
return s.toVolume(v), nil
}
// WriteAt writes a value to the slice provided in the byte order provided
func (s Sample8BitSigned) WriteAt(data []byte, ofs int64, v volume.Volume) error {
if len(data) <= int(ofs) {
return io.EOF
}
if ofs < 0 {
ofs = 0
}
data[ofs] = uint8(s.fromVolume(v))
return nil
}
// Write writes a value to the Writer provided in the byte order provided
func (s Sample8BitSigned) Write(out io.Writer, v volume.Volume) error {
return binary.Write(out, binary.LittleEndian, s.fromVolume(v))
}
// Sample8BitUnsigned is an unsigned 8-bit sample
type Sample8BitUnsigned struct{}
// toVolume returns the volume value for the sample
func (Sample8BitUnsigned) toVolume(v uint8) volume.Volume {
return volume.Volume(int8(v-uint8(cSample8BitDataCoeff))) * cSample8BitVolumeCoeff
}
// fromVolume returns the volume value for the sample
func (Sample8BitUnsigned) fromVolume(v volume.Volume) uint8 {
return uint8(v.ToUintSample(8))
}
// Size returns the size of the sample in bytes
func (Sample8BitUnsigned) Size() int {
return cSample8BitBytes
}
// ReadAt reads a value from the slice provided in the byte order provided
func (s Sample8BitUnsigned) ReadAt(data []byte, ofs int64) (volume.Volume, error) {
if len(data) <= int(ofs) {
return 0, io.EOF
}
if ofs < 0 {
ofs = 0
}
v := uint8(data[ofs])
return s.toVolume(v), nil
}
// WriteAt writes a value to the slice provided in the byte order provided
func (s Sample8BitUnsigned) WriteAt(data []byte, ofs int64, v volume.Volume) error {
if len(data) <= int(ofs) {
return io.EOF
}
if ofs < 0 {
ofs = 0
}
data[ofs] = s.fromVolume(v)
return nil
}
// Write writes a value to the Writer provided in the byte order provided
func (s Sample8BitUnsigned) Write(out io.Writer, v volume.Volume) error {
return binary.Write(out, binary.LittleEndian, s.fromVolume(v))
}