-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaudio.go
122 lines (97 loc) · 2.74 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
package stegano
import (
"fmt"
c "github.com/scott-mescudi/stegano/compression"
u "github.com/scott-mescudi/stegano/pkg"
)
// EmbedDataIntoWAVWithDepth embeds compressed data into a WAV file with a specified bit depth.
func (s *AudioEmbedHandler) EmbedIntoWAVWithDepth(audioFilename, outputFilename string, data []byte, bitDepth uint8) error {
if bitDepth >= 8 {
return ErrDepthOutOfRange
}
nd, err := c.CompressZSTD(data)
if err != nil {
return err
}
decoder := LoadAudioData(audioFilename)
buffer, err := decoder.FullPCMBuffer()
if err != nil {
return err
}
buffer, err = u.EmbedDataWithDepthAudio(buffer, nd, bitDepth)
if err != nil {
return err
}
err = SaveAudioToFile(outputFilename, decoder, buffer)
if err != nil {
return err
}
return nil
}
// ExtractDataFromWAVWithDepth extracts compressed data from a WAV file with a specified bit depth.
func (s *AudioExtractHandler) ExtractFromWAVWithDepth(audioFilename string, bitDepth uint8) ([]byte, error) {
if bitDepth >= 8 {
return nil, ErrDepthOutOfRange
}
decoder := LoadAudioData(audioFilename)
buffer, err := decoder.FullPCMBuffer()
if err != nil {
return nil, err
}
data := u.ExtractDataWithDepthAudio(buffer, bitDepth)
lenData, err := u.GetlenOfData(data)
if err != nil {
return nil, err
}
var moddedData = make([]byte, 0, lenData)
defer func() {
if r := recover(); r != nil {
moddedData = nil
err = fmt.Errorf("fatal error: %v", r)
}
}()
for i := 4; i < lenData+4; i++ {
if i >= len(data) {
return nil, fmt.Errorf("index out of range while accessing data: %d", i)
}
moddedData = append(moddedData, data[i])
}
nd, err := c.DecompressZSTD(moddedData)
if err != nil {
return nil, err
}
return nd, nil
}
// EmbedDataIntoWAVAtDepth embeds compressed data into a WAV file at a specified bit depth.
func (s *AudioEmbedHandler) EmbedIntoWAVAtDepth(audioFilename, outputFilename string, data []byte, bitDepth uint8) error {
if bitDepth >= 8 {
return ErrDepthOutOfRange
}
decoder := LoadAudioData(audioFilename)
buffer, err := decoder.FullPCMBuffer()
if err != nil {
return err
}
buffer, err = u.EmbedDataAtDepthAudio(buffer, data, bitDepth)
if err != nil {
return ErrInvalidData
}
err = SaveAudioToFile(outputFilename, decoder, buffer)
if err != nil {
return err
}
return nil
}
// ExtractDataFromWAVAtDepth extracts compressed data from a WAV file at a specified bit depth.
func (s *AudioExtractHandler) ExtractFromWAVAtDepth(audioFilename string, bitDepth uint8) ([]byte, error) {
if bitDepth >= 8 {
return nil, ErrDepthOutOfRange
}
decoder := LoadAudioData(audioFilename)
buffer, err := decoder.FullPCMBuffer()
if err != nil {
return nil, err
}
data := u.ExtractDataAtDepthAudio(buffer, bitDepth)
return data, nil
}