summaryrefslogtreecommitdiff
path: root/rhimport/normalizer.go
diff options
context:
space:
mode:
Diffstat (limited to 'rhimport/normalizer.go')
-rw-r--r--rhimport/normalizer.go30
1 files changed, 20 insertions, 10 deletions
diff --git a/rhimport/normalizer.go b/rhimport/normalizer.go
index 422acaa..0be9938 100644
--- a/rhimport/normalizer.go
+++ b/rhimport/normalizer.go
@@ -27,6 +27,7 @@ package rhimport
import (
"fmt"
"io"
+ "net/http"
"os"
"os/exec"
"path"
@@ -39,11 +40,13 @@ type ffmpegResult struct {
err error
}
-func runNormalizer(ctx *Context, src *os.File, size int64) (err error) {
+func runNormalizer(ctx *Context, res *Result, src *os.File, size int64) (err error) {
if ctx.DeleteSourceFile {
defer os.Remove(src.Name())
}
+ ctx.reportProgress(2, "normalizing", 0.0, float64(size))
+
basepath, filename := filepath.Split(src.Name())
ext := filepath.Ext(filename)
destName := strings.TrimSuffix(filename, ext) + "_normalized.flac"
@@ -65,6 +68,13 @@ func runNormalizer(ctx *Context, src *os.File, size int64) (err error) {
var written int64
for {
+ if ctx.isCanceled() {
+ ffmpeg.Process.Kill()
+ res.ResponseCode = http.StatusNoContent
+ res.ErrorString = "canceled"
+ return nil
+ }
+
var w int64
w, err = io.CopyN(ffstdin, src, 512*1024)
if err != nil {
@@ -74,17 +84,20 @@ func runNormalizer(ctx *Context, src *os.File, size int64) (err error) {
ctx.reportProgress(2, "normalizing", float64(written), float64(size))
}
ffstdin.Close()
- res := <-ffresult
- if res.err != nil {
- return fmt.Errorf("normalizer exited with error: %q, ffmpeg output: %s", res.err, res.output)
+ r := <-ffresult
+ if r.err != nil {
+ return fmt.Errorf("normalizer exited with error: %q, ffmpeg output: %s", r.err, r.output)
}
if err != nil && err != io.EOF {
return
}
+ ctx.reportProgress(2, "normalizing", float64(size), float64(size))
return nil
}
-func NormalizeFile(ctx *Context) (err error) {
+func NormalizeFile(ctx *Context) (res *Result, err error) {
+ res = &Result{ResponseCode: http.StatusOK}
+
if ctx.LoudnessCorr == 0.0 {
rhl.Println("NormalizeFile: skipping normalization since the gain = 0.0dB")
ctx.reportProgress(2, "normalizing", 1.0, 1.0)
@@ -101,13 +114,12 @@ func NormalizeFile(ctx *Context) (err error) {
size := int64(0)
if info, err := src.Stat(); err != nil {
- return err
+ return res, err
} else {
size = info.Size()
}
- ctx.reportProgress(2, "normalizing", 0.0, float64(size))
- if err = runNormalizer(ctx, src, size); err != nil {
+ if err = runNormalizer(ctx, res, src, size); err != nil {
rhl.Println("NormalizeFile error:", err)
if ctx.DeleteSourceFile {
os.Remove(ctx.SourceFile)
@@ -117,7 +129,5 @@ func NormalizeFile(ctx *Context) (err error) {
}
return
}
- ctx.reportProgress(2, "normalizing", float64(size), float64(size))
-
return
}