Files
aego/audio/mix/source.go
T
sanspie 50be5c04bc
ci-platforms / darwin amd64 smoke (push) Skipped
GitHub / linux amd64 native GitHub-hosted native CI: success
ci-platforms / windows amd64 smoke (push) Skipped
GitHub / windows amd64 native GitHub-hosted native CI: success
GitHub / darwin arm64 native GitHub-hosted native CI: success
GitHub / darwin amd64 native GitHub-hosted native CI: success
ci / checks (push) Failing after 1h21m34s
ci / native-authoring (push) Skipped
ci-platforms / darwin arm64 smoke (push) Skipped
github-native / gitea-pending (push) Skipped
github-native / darwin amd64 native (push) Skipped
github-native / linux amd64 native (push) Skipped
github-native / windows amd64 native (push) Skipped
github-native / darwin arm64 native (push) Skipped
github-native / gitea-result (push) Skipped
ci / build (amd64, linux, ubuntu-24.04) (push) Successful in 12m4s
ci / release (push) Skipped
Stage 12.5: Art Workspace and asset authoring
Add dockable Art tools, animation and previews, guarded asset workflows,
and the Web Asset Studio built on the shared Art Core. Expand English and
Russian documentation, interactive Try scenes, examples and editor icons.

Add audio waveform navigation, source analysis, guarded previews and a
browser Worker mixer. Preserve source changes and recovery with bounded
checks, and strengthen native, browser and interoperability CI.

Prepare version 0.12.5 and manually built native artifact candidates.
Fix the production site image demo build dependency.
Reuse immutable scene indices when capturing keyframes.
Wait for real Persist save/load completion in component tests.
Stabilize browser Game View captures outside pointer hover.
Aseprite interchange and persistent Audio EDL recovery remain limited;
the artifact workflow does not publish releases or change stable.
2026-11-15 10:37:50 +03:00

182 lines
3.6 KiB
Go

package mix
import "sync/atomic"
const streamCache = 1<<16 + 16
type Memory struct {
PCM []float32
Rate int
Channels int
LoopStart int64
LoopEnd int64
Loop bool
}
func (m *Memory) Frames() int64 {
if m.Channels <= 0 {
return 0
}
return int64(len(m.PCM) / m.Channels)
}
func (m *Memory) loopRange() (int64, int64) {
n := m.Frames()
s, e := m.LoopStart, m.LoopEnd
if e <= 0 || e > n {
e = n
}
if s < 0 || s >= e {
s = 0
}
return s, e
}
type Stream struct {
Ring *Ring
Rate int
Channels int
eof atomic.Bool
skipped atomic.Int64
hold atomic.Bool
wake chan struct{}
space chan struct{}
waiting atomic.Bool
remote atomic.Bool
cache []float32
start int64
n int
inUse bool
}
func NewStream(rate, channels, ringFrames int) *Stream {
if channels != 2 {
channels = 1
}
return &Stream{
Ring: NewRing(ringFrames, channels), Rate: rate, Channels: channels,
cache: make([]float32, streamCache*channels), wake: make(chan struct{}, 1), space: make(chan struct{}, 1),
}
}
const spaceShare = 4
// WaitSpace waits until the mixer has read a quarter of the ring, a skip or a hold arrives, or cancel closes; false means cancel.
func (s *Stream) WaitSpace(cancel <-chan struct{}) bool {
s.waiting.Store(true)
defer s.waiting.Store(false)
if s.Ring.Free() >= s.Ring.Cap()/spaceShare {
return true
}
select {
case <-cancel:
return false
case <-s.space:
case <-s.wake:
}
return true
}
func (s *Stream) drained() {
if s.waiting.Load() && s.Ring.Free() >= s.Ring.Cap()/spaceShare {
select {
case s.space <- struct{}{}:
default:
}
}
}
// TakeSkip returns the source frames the feeder must skip before it decodes again, and clears them.
func (s *Stream) TakeSkip() int64 {
return s.skipped.Swap(0)
}
// Held reports that the mixer skips the stream, so the feeder keeps its position and decodes nothing.
func (s *Stream) Held() bool {
return s.hold.Load()
}
// Wake receives when the mixer adds a skip or releases the hold.
func (s *Stream) Wake() <-chan struct{} {
return s.wake
}
func (s *Stream) signal() {
select {
case s.wake <- struct{}{}:
default:
}
}
func (s *Stream) release() {
if s.hold.Swap(false) {
s.signal()
}
}
// skip moves a voice to "to" without rendering: decoded frames are dropped from the cache and the ring, the rest is left to the feeder; it reports a stream that ended.
func (s *Stream) skip(to float64) bool {
ch := s.Channels
want := int64(to) - 1
if want <= s.start {
return false
}
if k := int(min(want-s.start, int64(s.n))); k > 0 {
copy(s.cache, s.cache[k*ch:s.n*ch])
s.n -= k
s.start += int64(k)
}
if rest := want - s.start; rest > 0 {
eof := s.eof.Load()
got := s.Ring.Discard(rest)
s.start += got
if rest -= got; rest > 0 {
if eof && s.Ring.Len() == 0 {
return true
}
s.skipped.Add(rest)
s.start += rest
}
}
s.hold.Store(true)
s.signal()
return false
}
func (s *Stream) SetEOF() {
s.eof.Store(true)
}
func (s *Stream) EOF() bool {
return s.eof.Load()
}
func (s *Stream) reset() {
s.skipped.Store(0)
s.hold.Store(false)
s.start, s.n = -1, 1
clear(s.cache[:s.Channels])
}
func (s *Stream) fill(pos, step float64, frames int) {
ch := s.Channels
if d := int64(pos) - 1 - s.start; d > 0 {
k := int(min(d, int64(s.n)))
copy(s.cache, s.cache[k*ch:s.n*ch])
s.n -= k
s.start += int64(k)
}
need := min(int(int64(pos+step*float64(frames))+3-s.start), streamCache)
if s.n < need {
s.n += s.Ring.Read(s.cache[s.n*ch : need*ch])
s.drained()
}
}
func (s *Stream) at(k, c int) float32 {
if k < 0 || k >= s.n {
return 0
}
return s.cache[k*s.Channels+c]
}