Skip to content

Commit

Permalink
Merge pull request #203 from oakmound/feature/v4-entities
Browse files Browse the repository at this point in the history
Feature/v4 entities
  • Loading branch information
200sc committed Apr 12, 2022
2 parents 225ad13 + 21c4487 commit 8168fee
Show file tree
Hide file tree
Showing 99 changed files with 1,278 additions and 3,880 deletions.
2 changes: 2 additions & 0 deletions alg/range/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package range provides helper constructs to represent ranges of values, to poll from or clamp to
package arange
39 changes: 18 additions & 21 deletions audio/format/ceol/ceol.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"strconv"
"strings"
"time"

"github.com/oakmound/oak/v3/audio/sequence"
"github.com/oakmound/oak/v3/audio/synth"
)

// Raw Ceol types, holds all information in ceol file
Expand Down Expand Up @@ -66,24 +63,24 @@ type Filter struct {

// ChordPattern converts a Ceol's patterns and arrangement into a playable chord
// pattern for sequences
func (c Ceol) ChordPattern() sequence.ChordPattern {
chp := sequence.ChordPattern{}
chp.Pitches = make([][]synth.Pitch, c.PatternLength*len(c.Arrangement))
chp.Holds = make([][]time.Duration, c.PatternLength*len(c.Arrangement))
for i, m := range c.Arrangement {
for _, p := range m {
if p != -1 {
for _, n := range c.Patterns[p].Notes {
chp.Pitches[n.Offset+i*c.PatternLength] =
append(chp.Pitches[n.Offset+i*c.PatternLength], synth.NoteFromIndex(n.PitchIndex))
chp.Holds[n.Offset+i*c.PatternLength] =
append(chp.Holds[n.Offset+i*c.PatternLength], DurationFromQuarters(c.Bpm, n.Length))
}
}
}
}
return chp
}
// func (c Ceol) ChordPattern() sequence.ChordPattern {
// chp := sequence.ChordPattern{}
// chp.Pitches = make([][]synth.Pitch, c.PatternLength*len(c.Arrangement))
// chp.Holds = make([][]time.Duration, c.PatternLength*len(c.Arrangement))
// for i, m := range c.Arrangement {
// for _, p := range m {
// if p != -1 {
// for _, n := range c.Patterns[p].Notes {
// chp.Pitches[n.Offset+i*c.PatternLength] =
// append(chp.Pitches[n.Offset+i*c.PatternLength], synth.NoteFromIndex(n.PitchIndex))
// chp.Holds[n.Offset+i*c.PatternLength] =
// append(chp.Holds[n.Offset+i*c.PatternLength], DurationFromQuarters(c.Bpm, n.Length))
// }
// }
// }
// }
// return chp
// }

// DurationFromQuarters should not be here, should be in a package
// managing bpm and time
Expand Down
2 changes: 2 additions & 0 deletions audio/format/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package format provides audio file and format parsers
package format
44 changes: 42 additions & 2 deletions audio/pcm/interface.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package pcm provides a interface for interacting with PCM audio streams
package pcm

import "io"
Expand Down Expand Up @@ -45,6 +46,7 @@ type Format struct {
// 1 for mono and 2 for stereo.
Channels uint16
// Bits determines how many bits a single sample value takes up. 8, 16, and 32 are common values.
// TODO: Do we need LE vs BE, float vs int representation?
Bits uint16
}

Expand All @@ -55,6 +57,44 @@ func (f Format) PCMFormat() Format {

// BytesPerSecond returns how many bytes this format would be encoded into per second in an audio stream.
func (f Format) BytesPerSecond() uint32 {
blockAlign := f.Channels * f.Bits / 8
return f.SampleRate * uint32(blockAlign)
return f.SampleRate * uint32(f.SampleSize())
}

func (f Format) SampleSize() int {
return int(f.Channels) * int(f.Bits/8)
}

// ReadFloat reads a single sample from an audio stream, respecting bits and channels:
// f.Bits / 8 bytes * f.Channels bytes will be read from b, and this count will be returned as 'read'.
// the length of values will be equal to f.Channels, if no error is returned. If an error is returned,
// it will be io.ErrUnexpectedEOF. If bits is an unexpected value
func (f Format) SampleFloat(b []byte) (values []float64, read int, err error) {
values = make([]float64, 0, f.Channels)
read = f.SampleSize()
if len(b) < read {
return nil, 0, io.ErrUnexpectedEOF
}
_ = b[read-1]
switch f.Bits {
case 8:
for i := 0; i < int(f.Channels); i++ {
v := int8(b[i])
values = append(values, float64(v))
}
case 16:
for i := 0; i < int(f.Channels)*2; i += 2 {
v := int16(b[i]) +
int16(b[i+1])<<8
values = append(values, float64(v))
}
case 32:
for i := 0; i < int(f.Channels)*4; i += 4 {
v := int32(b[i]) +
int32(b[i+1])<<8 +
int32(b[i+2])<<16 +
int32(b[i+3])<<24
values = append(values, float64(v))
}
}
return
}
1 change: 1 addition & 0 deletions audio/play.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package audio provides utilities for playing or writing audio streams to OS consumers
package audio

import (
Expand Down
36 changes: 0 additions & 36 deletions audio/sequence/chordPattern.go

This file was deleted.

20 changes: 0 additions & 20 deletions audio/sequence/generator.go

This file was deleted.

76 changes: 0 additions & 76 deletions audio/sequence/holdPattern.go

This file was deleted.

24 changes: 0 additions & 24 deletions audio/sequence/length.go

This file was deleted.

25 changes: 0 additions & 25 deletions audio/sequence/loop.go

This file was deleted.

74 changes: 0 additions & 74 deletions audio/sequence/pitchPattern.go

This file was deleted.

0 comments on commit 8168fee

Please sign in to comment.