From 10932167534a177f7dbee8461da08ad8f7b3596d Mon Sep 17 00:00:00 2001 From: hollowness-inside-pc Date: Tue, 23 Apr 2024 09:55:21 +1000 Subject: [PATCH 1/2] Explicitly allocate memory for slice --- wave/writer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wave/writer.go b/wave/writer.go index a87082b..04833e9 100644 --- a/wave/writer.go +++ b/wave/writer.go @@ -79,7 +79,6 @@ func int32ToBytes(i int) []byte { } func framesToData(frames []Frame, wfmt WaveFmt) (WaveData, []byte) { - b := []byte{} raw := samplesToRawData(frames, wfmt) // We receive frames but have to store the size of the samples @@ -88,6 +87,7 @@ func framesToData(frames []Frame, wfmt WaveFmt) (WaveData, []byte) { subBytes := int32ToBytes(subchunksize) // construct the data part.. + b := make([]byte, 0, 4+4+len(raw)) b = append(b, Subchunk2ID...) b = append(b, subBytes...) b = append(b, raw...) From 662a0f35a2a70884658d23595bc9f881ad52c195 Mon Sep 17 00:00:00 2001 From: hollowness-inside-pc Date: Tue, 23 Apr 2024 10:09:36 +1000 Subject: [PATCH 2/2] Explicitly allocate memory for slices --- wave/writer.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/wave/writer.go b/wave/writer.go index a87082b..e090a8c 100644 --- a/wave/writer.go +++ b/wave/writer.go @@ -79,7 +79,6 @@ func int32ToBytes(i int) []byte { } func framesToData(frames []Frame, wfmt WaveFmt) (WaveData, []byte) { - b := []byte{} raw := samplesToRawData(frames, wfmt) // We receive frames but have to store the size of the samples @@ -88,6 +87,7 @@ func framesToData(frames []Frame, wfmt WaveFmt) (WaveData, []byte) { subBytes := int32ToBytes(subchunksize) // construct the data part.. + b := make([]byte, 0, 8+len(raw)) b = append(b, Subchunk2ID...) b = append(b, subBytes...) b = append(b, raw...) @@ -103,7 +103,7 @@ func framesToData(frames []Frame, wfmt WaveFmt) (WaveData, []byte) { func floatToBytes(f float64, nBytes int) []byte { bits := math.Float64bits(f) - bs := make([]byte, 8) + bs := make([]byte, 0, 8) binary.LittleEndian.PutUint64(bs, bits) // trim padding switch nBytes { @@ -134,8 +134,6 @@ func rescaleFrame(s Frame, bits int) int { } func fmtToBytes(wfmt WaveFmt) []byte { - b := []byte{} - subchunksize := int32ToBytes(wfmt.Subchunk1Size) audioformat := int16ToBytes(wfmt.AudioFormat) numchans := int16ToBytes(wfmt.NumChannels) @@ -144,6 +142,7 @@ func fmtToBytes(wfmt WaveFmt) []byte { blockalign := int16ToBytes(wfmt.BlockAlign) bitsPerSample := int16ToBytes(wfmt.BitsPerSample) + b := make([]byte, 0, 23) b = append(b, wfmt.Subchunk1ID...) b = append(b, subchunksize...) b = append(b, audioformat...) @@ -159,11 +158,11 @@ func fmtToBytes(wfmt WaveFmt) []byte { // turn the sample to a valid header func createHeader(wd WaveData) []byte { // write chunkID - bits := []byte{} chunksize := 36 + wd.Subchunk2Size cb := int32ToBytes(chunksize) + bits := make([]byte, 0, 12) bits = append(bits, ChunkID...) // in theory switch on endianness.. bits = append(bits, cb...) bits = append(bits, WaveID...)