Renaming, fix frames/bytes confusion

This commit is contained in:
Rob Watson 2021-11-12 13:36:26 +01:00
parent 99659adb9e
commit 650382fb39
2 changed files with 25 additions and 28 deletions

View File

@ -18,11 +18,10 @@ type GetAudioProgressReader interface {
Close() error Close() error
} }
// fetchAudioProgressReader accepts a byte stream containing little endian // getAudioProgressReader accepts a byte stream containing little endian
// signed int16s and, given a target number of bins, emits a stream of peaks // signed int16s and, given a target number of bins, emits a stream of peaks
// corresponding to each channel of the audio data. // corresponding to each channel of the audio data.
type fetchAudioProgressReader struct { type getAudioProgressReader struct {
byteOrder binary.ByteOrder
framesExpected int64 framesExpected int64
channels int channels int
framesPerBin int framesPerBin int
@ -36,9 +35,8 @@ type fetchAudioProgressReader struct {
} }
// TODO: validate inputs, debugging is confusing otherwise // TODO: validate inputs, debugging is confusing otherwise
func newGetAudioProgressReader(byteOrder binary.ByteOrder, framesExpected int64, channels, numBins int) *fetchAudioProgressReader { func newGetAudioProgressReader(framesExpected int64, channels, numBins int) *getAudioProgressReader {
return &fetchAudioProgressReader{ return &getAudioProgressReader{
byteOrder: byteOrder,
channels: channels, channels: channels,
framesExpected: framesExpected, framesExpected: framesExpected,
framesPerBin: int(math.Ceil(float64(framesExpected) / float64(numBins))), framesPerBin: int(math.Ceil(float64(framesExpected) / float64(numBins))),
@ -49,16 +47,16 @@ func newGetAudioProgressReader(byteOrder binary.ByteOrder, framesExpected int64,
} }
} }
func (w *fetchAudioProgressReader) Abort(err error) { func (w *getAudioProgressReader) Abort(err error) {
w.errorChan <- err w.errorChan <- err
} }
func (w *fetchAudioProgressReader) Close() error { func (w *getAudioProgressReader) Close() error {
close(w.progress) close(w.progress)
return nil return nil
} }
func (w *fetchAudioProgressReader) Read() (GetAudioProgress, error) { func (w *getAudioProgressReader) Read() (GetAudioProgress, error) {
for { for {
select { select {
case progress, ok := <-w.progress: case progress, ok := <-w.progress:
@ -72,7 +70,7 @@ func (w *fetchAudioProgressReader) Read() (GetAudioProgress, error) {
} }
} }
func (w *fetchAudioProgressReader) Write(p []byte) (int, error) { func (w *getAudioProgressReader) Write(p []byte) (int, error) {
// expand our target slice if it is of insufficient size: // expand our target slice if it is of insufficient size:
numSamples := len(p) / SizeOfInt16 numSamples := len(p) / SizeOfInt16
if len(w.samples) < numSamples { if len(w.samples) < numSamples {
@ -81,7 +79,7 @@ func (w *fetchAudioProgressReader) Write(p []byte) (int, error) {
samples := w.samples[:numSamples] samples := w.samples[:numSamples]
if err := binary.Read(bytes.NewReader(p), w.byteOrder, samples); err != nil { if err := binary.Read(bytes.NewReader(p), binary.LittleEndian, samples); err != nil {
return 0, fmt.Errorf("error parsing samples: %v", err) return 0, fmt.Errorf("error parsing samples: %v", err)
} }
@ -106,7 +104,7 @@ func (w *fetchAudioProgressReader) Write(p []byte) (int, error) {
return len(p), nil return len(p), nil
} }
func (w *fetchAudioProgressReader) nextBin() { func (w *getAudioProgressReader) nextBin() {
var progress GetAudioProgress var progress GetAudioProgress
// TODO: avoid an allocation? // TODO: avoid an allocation?
progress.Peaks = append(progress.Peaks, w.currPeaks...) progress.Peaks = append(progress.Peaks, w.currPeaks...)

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"database/sql" "database/sql"
"encoding/binary"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -259,16 +258,15 @@ func (s *MediaSetService) getAudioFromS3(ctx context.Context, mediaSet store.Med
return nil, fmt.Errorf("error getting object from s3: %v", err) return nil, fmt.Errorf("error getting object from s3: %v", err)
} }
fetchAudioProgressReader := newGetAudioProgressReader( getAudioProgressReader := newGetAudioProgressReader(
binary.BigEndian,
int64(mediaSet.AudioFrames.Int64), int64(mediaSet.AudioFrames.Int64),
int(mediaSet.AudioChannels), int(mediaSet.AudioChannels),
numBins, numBins,
) )
state := getAudioFromS3State{ state := getAudioFromS3State{
fetchAudioProgressReader: fetchAudioProgressReader, getAudioProgressReader: getAudioProgressReader,
s3Reader: NewModuloBufReader(output.Body, int(mediaSet.AudioChannels)*SizeOfInt16), s3Reader: NewModuloBufReader(output.Body, int(mediaSet.AudioChannels)*SizeOfInt16),
} }
go state.run(ctx) go state.run(ctx)
@ -276,7 +274,7 @@ func (s *MediaSetService) getAudioFromS3(ctx context.Context, mediaSet store.Med
} }
type getAudioFromS3State struct { type getAudioFromS3State struct {
*fetchAudioProgressReader *getAudioProgressReader
s3Reader io.ReadCloser s3Reader io.ReadCloser
} }
@ -345,20 +343,20 @@ func (s *MediaSetService) getAudioFromYoutube(ctx context.Context, mediaSet stor
s3Key := fmt.Sprintf("media_sets/%s/audio.raw", mediaSet.ID) s3Key := fmt.Sprintf("media_sets/%s/audio.raw", mediaSet.ID)
uploader := newMultipartUploader(s.s3) uploader := newMultipartUploader(s.s3)
fetchAudioProgressReader := newGetAudioProgressReader( getAudioProgressReader := newGetAudioProgressReader(
binary.LittleEndian,
int64(mediaSet.AudioFramesApprox), int64(mediaSet.AudioFramesApprox),
format.AudioChannels, format.AudioChannels,
numBins, numBins,
) )
state := getAudioFromYoutubeState{ state := getAudioFromYoutubeState{
fetchAudioProgressReader: fetchAudioProgressReader, getAudioProgressReader: getAudioProgressReader,
ffmpegReader: ffmpegReader, ffmpegReader: ffmpegReader,
uploader: uploader, uploader: uploader,
s3Bucket: s3Bucket, s3Bucket: s3Bucket,
s3Key: s3Key, s3Key: s3Key,
store: s.store, store: s.store,
channels: format.AudioChannels,
} }
go state.run(ctx, mediaSet.ID) go state.run(ctx, mediaSet.ID)
@ -366,12 +364,13 @@ func (s *MediaSetService) getAudioFromYoutube(ctx context.Context, mediaSet stor
} }
type getAudioFromYoutubeState struct { type getAudioFromYoutubeState struct {
*fetchAudioProgressReader *getAudioProgressReader
ffmpegReader *ffmpegReader ffmpegReader *ffmpegReader
uploader *multipartUploader uploader *multipartUploader
s3Bucket, s3Key string s3Bucket, s3Key string
store Store store Store
channels int
} }
func (s *getAudioFromYoutubeState) run(ctx context.Context, mediaSetID uuid.UUID) { func (s *getAudioFromYoutubeState) run(ctx context.Context, mediaSetID uuid.UUID) {
@ -399,7 +398,7 @@ func (s *getAudioFromYoutubeState) run(ctx context.Context, mediaSetID uuid.UUID
ID: mediaSetID, ID: mediaSetID,
AudioS3Bucket: sqlString(s.s3Bucket), AudioS3Bucket: sqlString(s.s3Bucket),
AudioS3Key: sqlString(s.s3Key), AudioS3Key: sqlString(s.s3Key),
AudioFrames: sqlInt64(bytesUploaded), AudioFrames: sqlInt64(bytesUploaded / SizeOfInt16 / int64(s.channels)),
}) })
if updateErr != nil { if updateErr != nil {