-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathformat_bit64float.go
More file actions
55 lines (45 loc) · 1.32 KB
/
Copy pathformat_bit64float.go
File metadata and controls
55 lines (45 loc) · 1.32 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
package sampling
import (
"encoding/binary"
"io"
"math"
"github.com/gotracker/playback/mixing/volume"
)
const (
//cSample64BitFloatVolumeCoeff = volume.Volume(1)
cSample64BitFloatBytes = 8
)
// Sample64BitFloat is a 64-bit floating-point sample
type Sample64BitFloat struct {
byteOrder binary.ByteOrder
}
// Size returns the size of the sample in bytes
func (Sample64BitFloat) Size() int {
return cSample64BitFloatBytes
}
// ReadAt reads a value from the reader provided in the byte order provided
func (s Sample64BitFloat) ReadAt(data []byte, ofs int64) (volume.Volume, error) {
if len(data) <= int(ofs)+(cSample64BitFloatBytes-1) {
return 0, io.EOF
}
if ofs < 0 {
ofs = 0
}
f := math.Float64frombits(s.byteOrder.Uint64(data[ofs:]))
return volume.Volume(f), nil
}
// WriteAt writes a value to the slice provided in the byte order provided
func (s Sample64BitFloat) WriteAt(data []byte, ofs int64, v volume.Volume) error {
if len(data) <= int(ofs) {
return io.EOF
}
if ofs < 0 {
ofs = 0
}
s.byteOrder.PutUint64(data[ofs:], math.Float64bits(v.WithOverflowProtection()))
return nil
}
// Write writes a value to the Writer provided in the byte order provided
func (s Sample64BitFloat) Write(out io.Writer, v volume.Volume) error {
return binary.Write(out, s.byteOrder, math.Float64bits(v.WithOverflowProtection()))
}