@@ -18,7 +18,7 @@ import (
18
18
// StreamSeekCloser when you want to release the resources.
19
19
func Decode (rc io.ReadCloser ) (s beep.StreamSeekCloser , format beep.Format , err error ) {
20
20
d := decoder {rc : rc }
21
- defer func () { // hacky way to always close rsc if an error occured
21
+ defer func () { // hacky way to always close rsc if an error occurred
22
22
if err != nil {
23
23
d .rc .Close ()
24
24
}
@@ -29,7 +29,7 @@ func Decode(rc io.ReadCloser) (s beep.StreamSeekCloser, format beep.Format, err
29
29
return nil , beep.Format {}, errors .Wrap (err , "wav" )
30
30
}
31
31
if string (d .h .RiffMark [:]) != "RIFF" {
32
- return nil , beep.Format {}, errors . New ( fmt .Sprintf ("wav: missing RIFF at the beginning > %s" , string (d .h .RiffMark [:]) ))
32
+ return nil , beep.Format {}, fmt .Errorf ("wav: missing RIFF at the beginning > %s" , string (d .h .RiffMark [:]))
33
33
}
34
34
35
35
// READ Total file size
@@ -45,7 +45,7 @@ func Decode(rc io.ReadCloser) (s beep.StreamSeekCloser, format beep.Format, err
45
45
46
46
// check each formtypes
47
47
ft := [4 ]byte {0 , 0 , 0 , 0 }
48
- var fs int32 = 0
48
+ var fs int32
49
49
d .hsz = 4 + 4 + 4 // add size of (RiffMark + FileSize + WaveMark)
50
50
for string (ft [:]) != "data" {
51
51
if err = binary .Read (rc , binary .LittleEndian , ft [:]); err != nil {
@@ -69,13 +69,12 @@ func Decode(rc io.ReadCloser) (s beep.StreamSeekCloser, format beep.Format, err
69
69
}
70
70
if err := binary .Read (rc , binary .LittleEndian , & fmtchunk ); err != nil {
71
71
return nil , beep.Format {}, errors .New ("wav: missing format chunk body" )
72
- } else {
73
- d .h .NumChans = fmtchunk .NumChans
74
- d .h .SampleRate = fmtchunk .SampleRate
75
- d .h .ByteRate = fmtchunk .ByteRate
76
- d .h .BytesPerFrame = fmtchunk .BytesPerFrame
77
- d .h .BitsPerSample = fmtchunk .BitsPerSample
78
72
}
73
+ d .h .NumChans = fmtchunk .NumChans
74
+ d .h .SampleRate = fmtchunk .SampleRate
75
+ d .h .ByteRate = fmtchunk .ByteRate
76
+ d .h .BytesPerFrame = fmtchunk .BytesPerFrame
77
+ d .h .BitsPerSample = fmtchunk .BitsPerSample
79
78
80
79
// SubFormat is represented by GUID. Plain PCM is KSDATAFORMAT_SUBTYPE_PCM GUID.
81
80
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/ksmedia/ns-ksmedia-waveformatextensible
@@ -84,26 +83,24 @@ func Decode(rc io.ReadCloser) (s beep.StreamSeekCloser, format beep.Format, err
84
83
[8 ]byte {0x80 , 0x00 , 0x00 , 0xaa , 0x00 , 0x38 , 0x9b , 0x71 },
85
84
}
86
85
if fmtchunk .SubFormat != pcmguid {
87
- return nil , beep.Format {}, errors .New (
88
- fmt .Sprintf (
89
- "wav: unsupported sub format type - %08x-%04x-%04x-%s" ,
90
- fmtchunk .SubFormat .Data1 , fmtchunk .SubFormat .Data2 , fmtchunk .SubFormat .Data3 ,
91
- hex .EncodeToString (fmtchunk .SubFormat .Data4 [:]),
92
- ),
86
+ return nil , beep.Format {}, fmt .Errorf (
87
+ "wav: unsupported sub format type - %08x-%04x-%04x-%s" ,
88
+ fmtchunk .SubFormat .Data1 , fmtchunk .SubFormat .Data2 , fmtchunk .SubFormat .Data3 ,
89
+ hex .EncodeToString (fmtchunk .SubFormat .Data4 [:]),
93
90
)
94
91
}
95
92
} else {
96
93
// WAVEFORMAT or WAVEFORMATEX
97
94
fmtchunk := formatchunk {0 , 0 , 0 , 0 , 0 }
98
95
if err := binary .Read (rc , binary .LittleEndian , & fmtchunk ); err != nil {
99
96
return nil , beep.Format {}, errors .New ("wav: missing format chunk body" )
100
- } else {
101
- d .h .NumChans = fmtchunk .NumChans
102
- d .h .SampleRate = fmtchunk .SampleRate
103
- d .h .ByteRate = fmtchunk .ByteRate
104
- d .h .BytesPerFrame = fmtchunk .BytesPerFrame
105
- d .h .BitsPerSample = fmtchunk .BitsPerSample
106
97
}
98
+ d .h .NumChans = fmtchunk .NumChans
99
+ d .h .SampleRate = fmtchunk .SampleRate
100
+ d .h .ByteRate = fmtchunk .ByteRate
101
+ d .h .BytesPerFrame = fmtchunk .BytesPerFrame
102
+ d .h .BitsPerSample = fmtchunk .BitsPerSample
103
+
107
104
// it would be skipping cbSize (WAVEFORMATEX's last member).
108
105
if d .h .FormatSize > 16 {
109
106
trash := make ([]byte , d .h .FormatSize - 16 )
@@ -137,7 +134,7 @@ func Decode(rc io.ReadCloser) (s beep.StreamSeekCloser, format beep.Format, err
137
134
return nil , beep.Format {}, errors .New ("wav: missing data chunk marker" )
138
135
}
139
136
if d .h .FormatType != 1 && d .h .FormatType != - 2 {
140
- return nil , beep.Format {}, errors . New ( fmt .Sprintf ("wav: unsupported format type - %d" , d .h .FormatType ) )
137
+ return nil , beep.Format {}, fmt .Errorf ("wav: unsupported format type - %d" , d .h .FormatType )
141
138
}
142
139
if d .h .NumChans <= 0 {
143
140
return nil , beep.Format {}, errors .New ("wav: invalid number of channels (less than 1)" )
0 commit comments