You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
912 B
42 lines
912 B
package audio
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/go-audio/wav"
|
|
|
|
"dread.land/deepgram-demo/internal/metadata"
|
|
)
|
|
|
|
type AudioServer struct {
|
|
Storage *AudioMemoryStorage
|
|
Metadata *metadata.MetadataMemoryStorage
|
|
}
|
|
|
|
func (a *AudioServer) Upload(filename string, contents []byte) error {
|
|
fmt.Println(a)
|
|
|
|
// extract duration and save as metadata
|
|
r := bytes.NewReader(contents)
|
|
decoder := wav.NewDecoder(r)
|
|
if !decoder.IsValidFile() {
|
|
return errors.New("invalid wav file")
|
|
}
|
|
dur, err := decoder.Duration()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = a.Metadata.Put(metadata.AudioMetadata{FileName: filename, Duration: int(dur.Milliseconds())})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = a.Storage.put(AudioFile{FileName: filename, Contents: contents})
|
|
return err
|
|
}
|
|
|
|
func (a *AudioServer) Download(filename string) ([]byte, error) {
|
|
file, err := a.Storage.get(filename)
|
|
return file.Contents, err
|
|
}
|
|
|