This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
audio.go
executable file
·163 lines (131 loc) · 3.67 KB
/
audio.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
package reisen
// #cgo pkg-config: libavformat libavcodec libavutil libswresample
// #include <libavcodec/avcodec.h>
// #include <libavformat/avformat.h>
// #include <libavutil/avutil.h>
// #include <libswresample/swresample.h>
import "C"
import (
"fmt"
"unsafe"
)
const (
// StandardChannelCount is used for
// audio conversion while decoding
// audio frames.
StandardChannelCount = 2
)
// AudioStream is a stream containing
// audio frames consisting of audio samples.
type AudioStream struct {
baseStream
swrCtx *C.SwrContext
buffer *C.uint8_t
bufferSize C.int
}
// ChannelCount returns the number of channels
// (1 for mono, 2 for stereo, etc.).
func (audio *AudioStream) ChannelCount() int {
return int(audio.codecParams.channels)
}
// SampleRate returns the sample rate of the
// audio stream.
func (audio *AudioStream) SampleRate() int {
return int(audio.codecParams.sample_rate)
}
// FrameSize returns the number of samples
// contained in one frame of the audio.
func (audio *AudioStream) FrameSize() int {
return int(audio.codecParams.frame_size)
}
// Open opens the audio stream to decode
// audio frames and samples from it.
func (audio *AudioStream) Open() error {
err := audio.open()
if err != nil {
return err
}
audio.swrCtx = C.swr_alloc_set_opts(nil,
C.AV_CH_FRONT_LEFT|C.AV_CH_FRONT_RIGHT,
C.AV_SAMPLE_FMT_DBL, audio.codecCtx.sample_rate,
channelLayout(audio), audio.
codecCtx.sample_fmt, audio.codecCtx.
sample_rate, 0, nil)
if audio.swrCtx == nil {
return fmt.Errorf(
"couldn't allocate an SWR context")
}
status := C.swr_init(audio.swrCtx)
if status < 0 {
return fmt.Errorf(
"%d: couldn't initialize the SWR context", status)
}
audio.buffer = nil
return nil
}
// ReadFrame reads a new frame from the stream.
func (audio *AudioStream) ReadFrame() (Frame, bool, error) {
return audio.ReadAudioFrame()
}
// ReadAudioFrame reads a new audio frame from the stream.
func (audio *AudioStream) ReadAudioFrame() (*AudioFrame, bool, error) {
ok, err := audio.read()
if err != nil {
return nil, false, err
}
if ok && audio.skip {
return nil, true, nil
}
// No more data.
if !ok {
return nil, false, nil
}
maxBufferSize := C.av_samples_get_buffer_size(
nil, StandardChannelCount,
audio.frame.nb_samples,
C.AV_SAMPLE_FMT_DBL, 1)
if maxBufferSize < 0 {
return nil, false, fmt.Errorf(
"%d: couldn't get the max buffer size", maxBufferSize)
}
if maxBufferSize > audio.bufferSize {
C.av_free(unsafe.Pointer(audio.buffer))
audio.buffer = nil
}
if audio.buffer == nil {
audio.buffer = (*C.uint8_t)(unsafe.Pointer(
C.av_malloc(bufferSize(maxBufferSize))))
audio.bufferSize = maxBufferSize
if audio.buffer == nil {
return nil, false, fmt.Errorf(
"couldn't allocate an AV buffer")
}
}
gotSamples := C.swr_convert(audio.swrCtx,
&audio.buffer, audio.frame.nb_samples,
&audio.frame.data[0], audio.frame.nb_samples)
if gotSamples < 0 {
return nil, false, fmt.Errorf(
"%d: couldn't convert the audio frame", gotSamples)
}
data := C.GoBytes(unsafe.Pointer(
audio.buffer), maxBufferSize)
frame := newAudioFrame(audio,
int64(audio.frame.pts),
int(audio.frame.coded_picture_number),
int(audio.frame.display_picture_number), data)
return frame, true, nil
}
// Close closes the audio stream and
// stops decoding audio frames.
func (audio *AudioStream) Close() error {
err := audio.close()
if err != nil {
return err
}
C.av_free(unsafe.Pointer(audio.buffer))
audio.buffer = nil
C.swr_free(&audio.swrCtx)
audio.swrCtx = nil
return nil
}