Skip to content

Commit

Permalink
Merge pull request #126 from bcc-code/fix/minus-inf
Browse files Browse the repository at this point in the history
fix(audio-norm): Fix crash on -inf for silent audio files
  • Loading branch information
KillerX authored Nov 29, 2023
2 parents 3adb680 + 68f65c3 commit c3f6f9e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
7 changes: 7 additions & 0 deletions activities/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package activities

import (
"context"
"math"

"github.com/bcc-code/bccm-flows/common"
"github.com/bcc-code/bccm-flows/paths"
"github.com/bcc-code/bccm-flows/services/ffmpeg"
Expand Down Expand Up @@ -44,6 +46,11 @@ func AnalyzeEBUR128Activity(ctx context.Context, input AnalyzeEBUR128Params) (*c
out.SuggestedAdjustment = -0.9 - analyzeResult.TruePeak
}

// Don't suggest adjustments below .5 dB, or for peaks below -69 dBTP
if math.Abs(out.SuggestedAdjustment) < 0.5 || out.TruePeak <= -69 {
out.SuggestedAdjustment = 0.0
}

return out, nil
}

Expand Down
7 changes: 7 additions & 0 deletions utils/execute_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"os/exec"
"strings"
)

// ExecuteCmd executes the cmd and returns through outputCallback line-by-line before returning the whole stdout at the end.
Expand Down Expand Up @@ -86,6 +87,12 @@ func ExecuteAnalysisCmd(cmd *exec.Cmd, outputCallback func(string)) (string, err

}

// replace -Inf with -99 if the audio was silent
result = strings.ReplaceAll(result, "\"-inf\"", "-99")

// replace inf with 0 target_offset if the audio was silent
result = strings.ReplaceAll(result, "\"inf\"", "0")

err = cmd.Wait()
if err != nil {
return "", fmt.Errorf("execution failed error: %s", err.Error())
Expand Down
25 changes: 15 additions & 10 deletions workflows/normalize_audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/bcc-code/bccm-flows/activities"
"github.com/bcc-code/bccm-flows/common"
"github.com/bcc-code/bccm-flows/utils/workflows"
wfutils "github.com/bcc-code/bccm-flows/utils/workflows"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"
)
Expand Down Expand Up @@ -69,21 +69,26 @@ func NormalizeAudioLevelWorkflow(
}

adjustResult := &common.AudioResult{}
err = wfutils.ExecuteWithQueue(ctx, activities.AdjustAudioLevelActivity, activities.AdjustAudioLevelParams{
Adjustment: r128Result.SuggestedAdjustment,
InFilePath: filePath,
OutFilePath: outputFolder,
}).Get(ctx, adjustResult)
if err != nil {
return nil, err

// Don't adjust if the suggested adjustment is less than 0.01 Db
if r128Result.SuggestedAdjustment <= 0.01 {
err = wfutils.ExecuteWithQueue(ctx, activities.AdjustAudioLevelActivity, activities.AdjustAudioLevelParams{
Adjustment: r128Result.SuggestedAdjustment,
InFilePath: filePath,
OutFilePath: outputFolder,
}).Get(ctx, adjustResult)
if err != nil {
return nil, err
}
filePath = adjustResult.OutputPath
}

out.FilePath = adjustResult.OutputPath.Local()
out.FilePath = filePath.Local()

if params.PerformOutputAnalysis {
r128Result := &common.AnalyzeEBUR128Result{}
err = wfutils.ExecuteWithQueue(ctx, activities.AnalyzeEBUR128Activity, activities.AnalyzeEBUR128Params{
FilePath: adjustResult.OutputPath,
FilePath: filePath,
TargetLoudness: params.TargetLUFS,
}).Get(ctx, r128Result)
if err != nil {
Expand Down

0 comments on commit c3f6f9e

Please sign in to comment.