-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from EsefexBot/dev-api
Fixed "memory" leak which copied sound in memory for every instance playing
- Loading branch information
Showing
12 changed files
with
130 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package pcmutil | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
) | ||
|
||
// ReadPCM reads PCM data from r and stores it in buf. | ||
// buf must be a slice of int16s. | ||
// ReadPCM returns the number of bytes read and an error if any. | ||
// ReadPCM assumes that the data is in little endian. | ||
// if the io.Reader gives an EOF before the buffer is filled, ReadPCM returns io.EOF and the number of bytes read. | ||
// unlike binary.Read, ReadPCM will still write to buf even if the io.Reader gives an EOF before the buffer is filled. | ||
func ReadPCM(r io.Reader, buf *[]int16) (int, error) { | ||
for i := range *buf { | ||
err := binary.Read(r, binary.LittleEndian, &(*buf)[i]) | ||
if err != nil { | ||
return i, err | ||
} | ||
} | ||
|
||
return len(*buf), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package pcmutil | ||
|
||
type PcmSample int16 | ||
type Pcm []PcmSample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package audioprocessing | ||
|
||
import ( | ||
"io" | ||
"log" | ||
) | ||
|
||
type S16leReferenceReader struct { | ||
data *[]int16 | ||
cursor int | ||
} | ||
|
||
func (s *S16leReferenceReader) Read(p []byte) (n int, err error) { | ||
if s.cursor >= len(*s.data)*2 { | ||
log.Println("EOF") | ||
return 0, io.EOF | ||
} | ||
|
||
n = 0 | ||
|
||
for i := range p { | ||
b, err := s.getByte(s.cursor) | ||
if err != nil { | ||
break | ||
} | ||
|
||
p[i] = b | ||
s.cursor++ | ||
n++ | ||
} | ||
|
||
return n, nil | ||
} | ||
|
||
func (s *S16leReferenceReader) getByte(i int) (byte, error) { | ||
if i >= len(*s.data)*2 { | ||
return 0, io.EOF | ||
} | ||
|
||
if i%2 == 0 { | ||
return byte((*s.data)[i/2] & 0xff), nil | ||
} else { | ||
return byte((*s.data)[i/2] >> 8), nil | ||
} | ||
} | ||
|
||
func (s *S16leReferenceReader) Load(data *[]int16) { | ||
s.data = data | ||
s.cursor = 0 | ||
} | ||
|
||
func NewS16leReferenceReader() *S16leReferenceReader { | ||
return &S16leReferenceReader{} | ||
} | ||
|
||
func NewS16leReferenceReaderFromRef(pcm *[]int16) *S16leReferenceReader { | ||
reader := &S16leReferenceReader{} | ||
reader.Load(pcm) | ||
|
||
return reader | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package audioprocessing | ||
|
||
import ( | ||
"esefexapi/audioprocessing/pcmutil" | ||
"log" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestS16leReferenceReader(t *testing.T) { | ||
data := []int16{1, 2, 3, 4, 5, 6} | ||
|
||
reader := NewS16leReferenceReaderFromRef(&data) | ||
|
||
assert.Equal(t, data, *reader.data) | ||
|
||
// assert.Equal(t, byte(1), reader.getByte(0)) | ||
// assert.Equal(t, byte(0), reader.getByte(1)) | ||
// assert.Equal(t, byte(2), reader.getByte(2)) | ||
// assert.Equal(t, byte(0), reader.getByte(3)) | ||
|
||
int16buf := make([]int16, len(data)) | ||
n, err := pcmutil.ReadPCM(reader, &int16buf) | ||
assert.Nil(t, err) | ||
log.Printf("n: %d", n) | ||
|
||
assert.Equal(t, data, int16buf) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters