Skip to content

Commit

Permalink
Merge pull request #148 from oakmound/release/2.4.1
Browse files Browse the repository at this point in the history
Release/2.5.0
  • Loading branch information
200sc authored Feb 25, 2021
2 parents 828120f + 1fdc97e commit b80723a
Show file tree
Hide file tree
Showing 49 changed files with 1,279 additions and 237 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.13
- name: Set up Go 1.16
uses: actions/setup-go@v1
with:
go-version: 1.13
go-version: 1.16
id: go

- name: Check out code into the Go module directory
Expand Down
13 changes: 13 additions & 0 deletions alg/floatgeom/dir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package floatgeom

import "testing"

func TestDirMethods(t *testing.T) {
d := Dir2{10.0, 12.0}
if d.X() != 10.0 {
t.Fatalf("expected 10 for x, got %v", d.X())
}
if d.Y() != 12.0 {
t.Fatalf("expected 12 for y, got %v", d.Y())
}
}
13 changes: 13 additions & 0 deletions alg/intgeom/dir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package intgeom

import "testing"

func TestDirMethods(t *testing.T) {
d := Dir2{10, 12}
if d.X() != 10 {
t.Fatalf("expected 10 for x, got %v", d.X())
}
if d.Y() != 12 {
t.Fatalf("expected 12 for y, got %v", d.Y())
}
}
26 changes: 26 additions & 0 deletions alg/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,29 @@ func TestCumulativeWeights(t *testing.T) {
cum := CumulativeWeights(weights)
assert.Equal(t, []float64{1, 3, 6, 10, 15, 21, 28}, cum)
}

func TestWeightedChooseOne(t *testing.T) {
weights := []float64{0, 0, 0, 0, 0, 0, 1}
remaining := RemainingWeights(weights)
choice := WeightedChooseOne(remaining)
if choice != 6 {
t.Fatalf("Expected choice of 6, got %v", choice)
}
}

type float64er struct {
val float64
}

func (f *float64er) Float64() float64 {
return f.val
}

func TestWeightedChooseOneSeeded(t *testing.T) {
weights := []float64{1, 1, 1, 1, 1, 1, 1}
remaining := RemainingWeights(weights)
choice := WeightedChooseOneSeeded(remaining, &float64er{val: 0})
if choice != 6 {
t.Fatalf("Expected choice of 6, got %v", choice)
}
}
15 changes: 13 additions & 2 deletions audio/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
// required to filter it through a sound font.
type Audio struct {
*font.Audio
toStop audio.Audio
X, Y *float64
toStop audio.Audio
X, Y *float64
setVolume int32
}

// New returns an audio from a font, some audio data, and optional
Expand All @@ -30,6 +31,15 @@ func New(f *font.Font, d Data, coords ...*float64) *Audio {
return a
}

// SetVolume attempts to set the volume of the underlying OS audio.
func (a *Audio) SetVolume(v int32) error {
a.setVolume = v
if a.toStop != nil {
return a.toStop.SetVolume(v)
}
return nil
}

// Play begin's an audio's playback
func (a *Audio) Play() <-chan error {
a2, err := a.Copy()
Expand All @@ -45,6 +55,7 @@ func (a *Audio) Play() <-chan error {
return errChannel(err)
}
a.toStop = a4
a.toStop.SetVolume(a.setVolume)
return a4.Play()
}

Expand Down
9 changes: 7 additions & 2 deletions audio/globals.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package audio

import "github.com/200sc/klangsynthese/font"
import (
"sync"

"github.com/200sc/klangsynthese/font"
)

var (
loaded = make(map[string]Data)
loadedLock sync.RWMutex
loaded = make(map[string]Data)
// DefFont is the font used for default functions. It can be publicly
// modified to apply a default font to generated audios through def
// methods. If it is not modified, it is a font of zero filters.
Expand Down
111 changes: 85 additions & 26 deletions audio/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/200sc/klangsynthese/audio"
"github.com/200sc/klangsynthese/mp3"
"github.com/200sc/klangsynthese/wav"
"golang.org/x/sync/errgroup"

"github.com/oakmound/oak/v2/dlog"
"github.com/oakmound/oak/v2/fileutil"
Expand Down Expand Up @@ -49,70 +50,128 @@ func Get(filename string) (Data, error) {
// one stored in the loeaded map.
func Load(directory, filename string) (Data, error) {
dlog.Verb("Loading", directory, filename)
if !IsLoaded(filename) {
f, err := fileutil.Open(filepath.Join(directory, filename))
if err != nil {
return nil, err
}
defer f.Close()
var buffer audio.Audio
end := strings.ToLower(filename[len(filename)-4:])
switch end {
case ".wav":
buffer, err = wav.Load(f)
case ".mp3":
buffer, err = mp3.Load(f)
default:
return nil, oakerr.UnsupportedFormat{Format: end}
}
if err != nil {
return nil, err
}
loaded[filename] = buffer.(audio.FullAudio)
if data, ok := getLoaded(filename); ok {
return data, nil
}
return loaded[filename], nil
f, err := fileutil.Open(filepath.Join(directory, filename))
if err != nil {
return nil, err
}
defer f.Close()
var buffer audio.Audio
end := strings.ToLower(filename[len(filename)-4:])
switch end {
case ".wav":
buffer, err = wav.Load(f)
case ".mp3":
buffer, err = mp3.Load(f)
default:
return nil, oakerr.UnsupportedFormat{Format: end}
}
if err != nil {
return nil, err
}
data := buffer.(audio.FullAudio)
setLoaded(filename, data)
return data, nil
}

// Unload removes an element from the loaded map. If the element does not
// exist, it does nothing.
func Unload(filename string) {
loadedLock.Lock()
delete(loaded, filename)
loadedLock.Unlock()
}

// IsLoaded is shorthand for (if _, ok := loaded[filename]; ok)
func IsLoaded(filename string) bool {
loadedLock.RLock()
_, ok := loaded[filename]
loadedLock.RUnlock()
return ok
}

func getLoaded(filename string) (Data, bool) {
loadedLock.RLock()
data, ok := loaded[filename]
loadedLock.RUnlock()
return data, ok
}

func setLoaded(filename string, data Data) {
loadedLock.Lock()
loaded[filename] = data
loadedLock.Unlock()
}

// BatchLoad attempts to load all files within a given directory
// depending on their file ending (currently supporting .wav and .mp3)
func BatchLoad(baseFolder string) error {
return batchLoad(baseFolder, false)
}

// BlankBatchLoad acts like BatchLoad, but replaces all loaded assets
// with empty audio constructs. This is intended to reduce start-up
// times in development.
func BlankBatchLoad(baseFolder string) error {
return batchLoad(baseFolder, true)
}

func batchLoad(baseFolder string, blankOut bool) error {
files, err := fileutil.ReadDir(baseFolder)

if err != nil {
dlog.Error(err)
return err
}

var eg errgroup.Group
for _, file := range files {
if !file.IsDir() {
n := file.Name()
dlog.Verb(n)
switch strings.ToLower(n[len(n)-4:]) {
case ".wav", ".mp3":
dlog.Verb("loading file ", n)
_, err := Load(baseFolder, n)
if err != nil {
dlog.Error(err)
return err
}
eg.Go(func() error {
var err error
if blankOut {
dlog.Verb("blank loading file")
err = blankLoad(n)
} else {
_, err = Load(baseFolder, n)
}
if err != nil {
dlog.Error(err)
return err
}
return nil
})
default:
dlog.Error("Unsupported file ending for batchLoad: ", n)
}
}
}
err = eg.Wait()
dlog.Verb("Loading complete")
return err
}

func blankLoad(filename string) error {
mformat := audio.Format{
SampleRate: 44000,
Bits: 16,
Channels: 2,
}
buffer, err := audio.EncodeBytes(
audio.Encoding{
Format: mformat,
})
if err != nil {
return err
}
data := buffer.(audio.FullAudio)
setLoaded(filename, data)
return nil
}
Loading

0 comments on commit b80723a

Please sign in to comment.