feat(tracker): add menu item to export .wav

Also refactor the common functions for .wav export into base package so that both sointu-play and tracker can use same functions.
This commit is contained in:
vsariola
2021-04-17 14:24:05 +03:00
parent 7893c1d1ed
commit 1b4f1a8c5e
6 changed files with 163 additions and 81 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt"
"io/ioutil"
"path/filepath"
"time"
"gioui.org/app"
"gopkg.in/yaml.v3"
@ -82,3 +83,33 @@ func (t *Tracker) saveSong(filename string) bool {
t.SetChangedSinceSave(false)
return true
}
func (t *Tracker) exportWav(pcm16 bool) {
filename, err := dialog.File().Filter(".wav file", "wav").Title("Export .wav").Save()
if err != nil {
return
}
var extension = filepath.Ext(filename)
if extension == "" {
filename = filename + ".wav"
}
synth, err := t.synthService.Compile(t.Song().Patch)
if err != nil {
t.Alert.Update(fmt.Sprintf("Error compiling the patch during export: %v", err), Error, time.Second*3)
return
}
for i := 0; i < 32; i++ {
synth.Release(i)
}
data, _, err := sointu.Play(synth, t.Song()) // render the song to calculate its length
if err != nil {
t.Alert.Update(fmt.Sprintf("Error rendering the song during export: %v", err), Error, time.Second*3)
return
}
buffer, err := sointu.Wav(data, pcm16)
if err != nil {
t.Alert.Update(fmt.Sprintf("Error converting to .wav: %v", err), Error, time.Second*3)
return
}
ioutil.WriteFile(filename, buffer, 0644)
}