diff --git a/go.mod b/go.mod index f3405aa..7b45372 100644 --- a/go.mod +++ b/go.mod @@ -48,6 +48,7 @@ require ( github.com/mattn/go-colorable v0.1.11 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect + github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect github.com/pierrec/lz4/v4 v4.1.14 // indirect github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2 // indirect github.com/pkg/errors v0.9.1 // indirect diff --git a/go.sum b/go.sum index a31d3cc..f8b567a 100644 --- a/go.sum +++ b/go.sum @@ -117,6 +117,8 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6 github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/otiai10/mint v1.3.3 h1:7JgpsBaN0uMkyju4tbYHu0mnM55hNKVYLsXmwr15NQI= github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= +github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0= +github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y= github.com/pierrec/lz4/v4 v4.1.14 h1:+fL8AQEZtz/ijeNnpduH0bROTu0O3NZAlPjQxGn8LwE= github.com/pierrec/lz4/v4 v4.1.14/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2 h1:acNfDZXmm28D2Yg/c3ALnZStzNaZMSagpbr96vY6Zjc= diff --git a/pkg/lunii/writer.go b/pkg/lunii/writer.go index 9c0caf0..3b27724 100644 --- a/pkg/lunii/writer.go +++ b/pkg/lunii/writer.go @@ -12,6 +12,7 @@ import ( "time" cp "github.com/otiai10/copy" + "github.com/pbnjay/memory" yaml "gopkg.in/yaml.v3" ) @@ -132,9 +133,31 @@ func (device *Device) AddStudioPack(studioPack *StudioPack, updateChan *chan str deviceAudioDirectory := filepath.Join(tempPath, "sf", "000") os.MkdirAll(deviceAudioDirectory, 0700) + // for each audioindex start a goroutine to convert and write the audio + // throttle the number of goroutines depending on the ram available + + waitingTime := 500 * time.Millisecond // 500 milliseconds + scheduleMaxRetries := 20 // 10 seconds (20 * 500ms) + minMemory := uint64(500000000) // 500MB + for i, audio := range *audioIndex { + // wait until there is enough memory to convert the audio + tryCount := 0 + for memory.FreeMemory() < minMemory { + tryCount++ + if tryCount > scheduleMaxRetries { + return errors.New("Timing out : Not enough memory to convert the audio files") + } + // wait 500 milliseconds before checking again + time.Sleep(waitingTime) + } + + // add a coroutine to the wait-group wg.Add(1) go convertAndWriteAudio(*reader, deviceAudioDirectory, audio, i) + + // wait 500 milliseconds so that coroutines can start + time.Sleep(waitingTime) } wg.Wait()