Skip to content

Commit

Permalink
Remove dependency on SDL
Browse files Browse the repository at this point in the history
Signed-off-by: James Hamlin <[email protected]>
  • Loading branch information
jfhamlin committed Apr 14, 2024
1 parent 35a7251 commit 8000fd4
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 168 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21.3
toolchain go1.21.9

require (
github.com/ebitengine/purego v0.7.0
github.com/fsnotify/fsnotify v1.6.0
github.com/glojurelang/glojure v0.2.3-0.20240129062610-e04ecdbb91c9
github.com/go-audio/audio v1.0.0
Expand All @@ -15,7 +16,6 @@ require (
github.com/mewkiz/flac v1.0.10
github.com/oov/audio v0.0.0-20171004131523-88a2be6dbe38
github.com/stretchr/testify v1.8.4
github.com/veandco/go-sdl2 v0.4.38
github.com/wailsapp/wails/v2 v2.8.0
gitlab.com/gomidi/midi/v2 v2.0.30
gonum.org/v1/gonum v0.14.0
Expand Down Expand Up @@ -68,7 +68,7 @@ require (
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/image v0.12.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/d4l3k/messagediff v1.2.2-0.20190829033028-7e0a312ae40b/go.mod h1:Oozb
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ebitengine/purego v0.7.0 h1:HPZpl61edMGCEW6XK2nsR6+7AnJ3unUxpTZBkkIXnMc=
github.com/ebitengine/purego v0.7.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
Expand Down Expand Up @@ -148,8 +150,6 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/veandco/go-sdl2 v0.4.38 h1:lx8syOA2ccXlgViYkQe2Kn/4xt+p9mdd1Qc/yYMrmSo=
github.com/veandco/go-sdl2 v0.4.38/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofep6SNiAjY=
github.com/wailsapp/go-webview2 v1.0.10 h1:PP5Hug6pnQEAhfRzLCoOh2jJaPdrqeRgJKZhyYyDV/w=
github.com/wailsapp/go-webview2 v1.0.10/go.mod h1:Uk2BePfCRzttBBjFrBmqKGJd41P6QIHeV9kTgIeOZNo=
github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhwHs=
Expand Down Expand Up @@ -235,8 +235,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down
9 changes: 0 additions & 9 deletions pkg/aio/mic.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,10 @@ import (
"github.com/jfhamlin/muscrat/pkg/console"
"github.com/jfhamlin/muscrat/pkg/ugen"
"github.com/oov/audio/resampler"

// pprof
"net/http"
_ "net/http/pprof"
)

func init() {
portaudio.Initialize()

// start pprof
go func() {
fmt.Println(http.ListenAndServe("localhost:6060", nil))
}()
}

func chk(err error) {
Expand Down
117 changes: 10 additions & 107 deletions pkg/audio/audio.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
package audio

import (
"encoding/binary"
"fmt"
"math"
"sync"
"time"

"github.com/ebitengine/oto/v3"
"github.com/jfhamlin/muscrat/pkg/conf"
)

type (
Option func(*options)

options struct{}

bufferReader struct {
// circular buffer
buf []byte
pos int
count int
input chan []byte
}
)

const (
Expand All @@ -33,9 +20,7 @@ const (
)

var (
otoCtx *oto.Context
player *oto.Player
reader *bufferReader
myCtx *context
)

func SampleRate() int {
Expand All @@ -47,121 +32,39 @@ func NumChannels() int {
}

func Open(opts ...Option) error {
if otoCtx != nil {
panic("audio already open")
}

//bufferDur := 4 * time.Duration(float64(time.Second)*float64(conf.OutputBufferSize)/sampleRate)
bufferDur := time.Duration(0)
ctx, ready, err := oto.NewContext(&oto.NewContextOptions{
SampleRate: sampleRate,
ChannelCount: numChannels,
Format: oto.FormatFloat32LE,
BufferSize: bufferDur,
})
ctx, err := newContext(SampleRate(), NumChannels(), 4*numChannels*conf.OutputBufferSize)
if err != nil {
return err
}
<-ready

otoCtx = ctx
reader = &bufferReader{
// enough space for two output buffers
buf: make([]byte, 2*4*conf.OutputBufferSize*numChannels),
input: make(chan []byte, maxQueuedBuffers),
}
player = otoCtx.NewPlayer(reader)
player.SetBufferSize(4 * numChannels * conf.OutputBufferSize)

player.Play()
myCtx = ctx

return nil
}

func Close() {
if player != nil {
player.Close()
player = nil
otoCtx = nil
reader = nil
}

}

var (
pool = &sync.Pool{
New: func() interface{} {
return make([]byte, 4*numChannels*conf.OutputBufferSize)
return make([]float32, numChannels*conf.OutputBufferSize)
},
}

val = float32(-1)
cnt = 0
)

func QueueAudioFloat64(fbuf []float64) error {
if reader == nil {
if myCtx == nil {
panic("audio not open")
}

buf := pool.Get().([]byte)
if len(buf) != 4*len(fbuf) {
panic("unexpected buffer size")
}

for i, flt := range fbuf {
_ = flt
//f32 := float32(flt)
f32 := val
cnt++
if cnt > 100 {
val = -val
cnt = 0
}

floatBits := math.Float32bits(f32)
binary.LittleEndian.PutUint32(buf[4*i:], floatBits)
buf := pool.Get().([]float32)
for i := 0; i < len(fbuf); i++ {
buf[i] = float32(fbuf[i])
}

reader.input <- buf
myCtx.input <- buf

return nil
}

func (r *bufferReader) Read(p []byte) (int, error) {
if r.count == 0 {
r.fillBuffer()
}
if len(p) > r.count {
p = p[:r.count]
}
fmt.Println("reading", len(p), "bytes")

n := 0
for n < len(p) {
n = copy(p, r.buf[r.pos:])
r.pos = (r.pos + n) % len(r.buf)
}
r.count -= len(p)

return len(p), nil
}

func (r *bufferReader) fillBuffer() {
buf := <-r.input

if (len(r.buf)-r.pos)%4 != 0 {
panic("invalid buffer position")
}

insertPos := (r.pos + r.count) % len(r.buf)
for i := 0; i < len(buf); i += 4 {
r.buf[insertPos] = buf[i]
r.buf[insertPos+1] = buf[i+1]
r.buf[insertPos+2] = buf[i+2]
r.buf[insertPos+3] = buf[i+3]
insertPos = (insertPos + 4) % len(r.buf)
}
r.count += len(buf)

pool.Put(buf)
}
Loading

0 comments on commit 8000fd4

Please sign in to comment.