Skip to content
Snippets Groups Projects
Commit 0fad0762 authored by Travis Ralston's avatar Travis Ralston
Browse files

Move audio decoders for easier copy/paste

parent f7546a73
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@ package i
import (
"errors"
"github.com/faiface/beep"
"github.com/faiface/beep/flac"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/thumbnailing/m"
......@@ -24,10 +25,18 @@ func (d flacGenerator) matches(img []byte, contentType string) bool {
return contentType == "audio/flac"
}
func (d flacGenerator) GenerateThumbnail(b []byte, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*m.Thumbnail, error) {
func (d flacGenerator) decode(b []byte) (beep.StreamSeekCloser, beep.Format, error) {
audio, format, err := flac.Decode(util.ByteCloser(b))
if err != nil {
return nil, errors.New("flac: error decoding audio: " + err.Error())
return audio, format, errors.New("flac: error decoding audio: " + err.Error())
}
return audio, format, nil
}
func (d flacGenerator) GenerateThumbnail(b []byte, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*m.Thumbnail, error) {
audio, format, err := d.decode(b)
if err != nil {
return nil, err
}
defer audio.Close()
......@@ -35,9 +44,9 @@ func (d flacGenerator) GenerateThumbnail(b []byte, contentType string, width int
}
func (d flacGenerator) GetAudioData(b []byte, nKeys int, ctx rcontext.RequestContext) (*m.AudioInfo, error) {
audio, format, err := flac.Decode(util.ByteCloser(b))
audio, format, err := d.decode(b)
if err != nil {
return nil, errors.New("flac: error decoding audio: " + err.Error())
return nil, err
}
defer audio.Close()
......
......@@ -32,10 +32,18 @@ func (d mp3Generator) matches(img []byte, contentType string) bool {
return contentType == "audio/mpeg"
}
func (d mp3Generator) GenerateThumbnail(b []byte, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*m.Thumbnail, error) {
func (d mp3Generator) decode(b []byte) (beep.StreamSeekCloser, beep.Format, error) {
audio, format, err := mp3.Decode(util.ByteCloser(b))
if err != nil {
return nil, errors.New("mp3: error decoding audio: " + err.Error())
return audio, format, errors.New("mp3: error decoding audio: " + err.Error())
}
return audio, format, nil
}
func (d mp3Generator) GenerateThumbnail(b []byte, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*m.Thumbnail, error) {
audio, format, err := d.decode(b)
if err != nil {
return nil, err
}
defer audio.Close()
......@@ -43,9 +51,9 @@ func (d mp3Generator) GenerateThumbnail(b []byte, contentType string, width int,
}
func (d mp3Generator) GetAudioData(b []byte, nKeys int, ctx rcontext.RequestContext) (*m.AudioInfo, error) {
audio, format, err := mp3.Decode(util.ByteCloser(b))
audio, format, err := d.decode(b)
if err != nil {
return nil, errors.New("mp3: error decoding audio: " + err.Error())
return nil, err
}
defer audio.Close()
......
......@@ -3,6 +3,7 @@ package i
import (
"errors"
"github.com/faiface/beep"
"github.com/faiface/beep/vorbis"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/thumbnailing/m"
......@@ -24,10 +25,18 @@ func (d oggGenerator) matches(img []byte, contentType string) bool {
return contentType == "audio/ogg"
}
func (d oggGenerator) GenerateThumbnail(b []byte, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*m.Thumbnail, error) {
func (d oggGenerator) decode(b []byte) (beep.StreamSeekCloser, beep.Format, error) {
audio, format, err := vorbis.Decode(util.ByteCloser(b))
if err != nil {
return nil, errors.New("ogg: error decoding audio: " + err.Error())
return audio, format, errors.New("ogg: error decoding audio: " + err.Error())
}
return audio, format, nil
}
func (d oggGenerator) GenerateThumbnail(b []byte, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*m.Thumbnail, error) {
audio, format, err := d.decode(b)
if err != nil {
return nil, err
}
defer audio.Close()
......@@ -35,9 +44,9 @@ func (d oggGenerator) GenerateThumbnail(b []byte, contentType string, width int,
}
func (d oggGenerator) GetAudioData(b []byte, nKeys int, ctx rcontext.RequestContext) (*m.AudioInfo, error) {
audio, format, err := vorbis.Decode(util.ByteCloser(b))
audio, format, err := d.decode(b)
if err != nil {
return nil, errors.New("ogg: error decoding audio: " + err.Error())
return nil, err
}
defer audio.Close()
......
......@@ -3,6 +3,7 @@ package i
import (
"errors"
"github.com/faiface/beep"
"github.com/faiface/beep/wav"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/thumbnailing/m"
......@@ -24,10 +25,18 @@ func (d wavGenerator) matches(img []byte, contentType string) bool {
return contentType == "audio/wav"
}
func (d wavGenerator) GenerateThumbnail(b []byte, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*m.Thumbnail, error) {
func (d wavGenerator) decode(b []byte) (beep.StreamSeekCloser, beep.Format, error) {
audio, format, err := wav.Decode(util.ByteCloser(b))
if err != nil {
return nil, errors.New("wav: error decoding audio: " + err.Error())
return audio, format, errors.New("wav: error decoding audio: " + err.Error())
}
return audio, format, nil
}
func (d wavGenerator) GenerateThumbnail(b []byte, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*m.Thumbnail, error) {
audio, format, err := d.decode(b)
if err != nil {
return nil, err
}
defer audio.Close()
......@@ -35,9 +44,9 @@ func (d wavGenerator) GenerateThumbnail(b []byte, contentType string, width int,
}
func (d wavGenerator) GetAudioData(b []byte, nKeys int, ctx rcontext.RequestContext) (*m.AudioInfo, error) {
audio, format, err := wav.Decode(util.ByteCloser(b))
audio, format, err := d.decode(b)
if err != nil {
return nil, errors.New("wav: error decoding audio: " + err.Error())
return nil, err
}
defer audio.Close()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment