OnlineLibrary

Форк
0
124 строки · 2.6 Кб
1
package sonic
2

3
//#include <sonic.h>
4
import "C"
5

6
import (
7
	"errors"
8
	"io"
9
	"runtime"
10
	"unsafe"
11
)
12

13
type Stream struct {
14
	sampleSize int
15
	stream     C.sonicStream
16
}
17

18
func NewStream(sampleRate, numChannels int) *Stream {
19
	sonicStream := C.sonicCreateStream(C.int(sampleRate), C.int(numChannels))
20
	if sonicStream == nil {
21
		panic("sonicCreateStream returned NULL")
22
	}
23

24
	s := &Stream{
25
		sampleSize: numChannels * C.sizeof_short,
26
		stream:     sonicStream,
27
	}
28

29
	runtime.SetFinalizer(s, func(s *Stream) { C.sonicDestroyStream(s.stream) })
30
	return s
31
}
32

33
func (s *Stream) Write(data []byte) (int, error) {
34
	nSamples := len(data) / s.sampleSize
35
	if nSamples == 0 {
36
		return 0, nil
37
	}
38
	ok := C.sonicWriteShortToStream(s.stream, (*C.short)(unsafe.Pointer(&data[0])), C.int(nSamples))
39
	if ok == 0 {
40
		return 0, errors.New("memory realloc failed")
41
	}
42
	return nSamples * s.sampleSize, nil
43
}
44

45
func (s *Stream) Read(data []byte) (int, error) {
46
	nSamples := len(data) / s.sampleSize
47
	if nSamples == 0 {
48
		return 0, io.ErrShortBuffer
49
	}
50
	readSamples := C.sonicReadShortFromStream(s.stream, (*C.short)(unsafe.Pointer(&data[0])), C.int(nSamples))
51
	if readSamples == 0 {
52
		return 0, io.EOF
53
	}
54
	return int(readSamples) * s.sampleSize, nil
55
}
56

57
func (s *Stream) SamplesAvailable() int {
58
	nSamples := C.sonicSamplesAvailable(s.stream)
59
	return int(nSamples)
60
}
61

62
func (s *Stream) Speed() float64 {
63
	speed := C.sonicGetSpeed(s.stream)
64
	return float64(speed)
65
}
66

67
func (s *Stream) SetSpeed(speed float64) {
68
	C.sonicSetSpeed(s.stream, C.float(speed))
69
}
70

71
func (s *Stream) Pitch() float64 {
72
	pitch := C.sonicGetPitch(s.stream)
73
	return float64(pitch)
74
}
75

76
func (s *Stream) SetPitch(pitch float64) {
77
	C.sonicSetPitch(s.stream, C.float(pitch))
78
}
79

80
func (s *Stream) Rate() float64 {
81
	rate := C.sonicGetRate(s.stream)
82
	return float64(rate)
83
}
84

85
func (s *Stream) SetRate(rate float64) {
86
	C.sonicSetRate(s.stream, C.float(rate))
87
}
88

89
func (s *Stream) Volume() float64 {
90
	volume := C.sonicGetVolume(s.stream)
91
	return float64(volume)
92
}
93

94
func (s *Stream) SetVolume(volume float64) {
95
	C.sonicSetVolume(s.stream, C.float(volume))
96
}
97

98
func (s *Stream) NumChannels() int {
99
	numChannels := C.sonicGetNumChannels(s.stream)
100
	return int(numChannels)
101
}
102

103
func (s *Stream) SetNumChannels(numChannels int) {
104
	C.sonicSetNumChannels(s.stream, C.int(numChannels))
105
}
106

107
func (s *Stream) ChordPitch() bool {
108
	if C.sonicGetChordPitch(s.stream) == 0 {
109
		return false
110
	}
111
	return true
112
}
113

114
func (s *Stream) SetChordPitch(useChordPitch bool) {
115
	if useChordPitch {
116
		C.sonicSetChordPitch(s.stream, C.int(1))
117
	} else {
118
		C.sonicSetChordPitch(s.stream, C.int(0))
119
	}
120
}
121

122
func (s *Stream) Flush() int {
123
	return int(C.sonicFlushStream(s.stream))
124
}
125

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.