Menu

#217 Dual mono -> mono

None
closed
nobody
None
5
2022-09-11
2018-12-21
No

Some DAWs (Cubase as an example) can't export multiple audio files depending on their channelcount. So we can export multiple tracks as mono or multiple tracks as stereo, but not mono outputs as mono tracks and stereo outputs as stereo tracks. We need to manually check which tracks have mono output (and it is of course time consuming, distracting and potentially generates mistakes).

SoX might be an excellent tool to help with this problem. It should scan through the stereo audio file and check if it really is stereo file or is it Left channel=Right channel file. If both channels share the same audio data, SoX drops one channel and leaves just the mono file. Doing it in batch mode on whole folder would be the fastest and effective method.

Discussion

  • Mans Rullgard

    Mans Rullgard - 2018-12-21

    You can check if a stero file actually has two identical channels using the command sox file.wav -n remix 1,2i stat and looking at the "Maximum amplitude" figure it prints.

     
  • Kamil Barański

    Kamil Barański - 2019-11-02

    Thanks, your remix/stat idea works great!

    I did a small script for windows (cmd) that's taking care of the dual mono files. Of course use at your own risk, as nothing guaranteed :)

    http://kamilbaranski.com/pliki/art/finddualmono.bat

     
    • isidroco

      isidroco - 2022-09-11

      Nice script Kamil, though comparing MaxAmp = 0 can be fooled with a crafted file with enough DC negative offset to just peak at 0, and will skip not perfect mono files (ie: dithered) . If first 4 chars of RMS amplitude = "0.00" will detect "almost" mono files whose L-R amplitude is less than -40 dB.
      Your batched slightly inspired me to "solve" this: https://sourceforge.net/p/sox/feature-requests/228/#0887
      Here's an improved batch with that idea, which can optionally take FLACs:

      @echo off
      set _soxExe=c:\AUDIO\UT\SOX\sox.exe
      set _tempFoundFileList=%TEMP%\_leftequalsright.tmp.txt
      rem
      rem findDualMono v22.0914  (c) kamilbaranski.com, isidrococo
      rem https://sourceforge.net/p/sox/feature-requests/217/
      rem
      rem find dual mono files and allows to convert them to mono stripping one channel
      rem INSTALL:
      rem - download SoX, install, set path on 2nd line in _soxExe
      rem - move finddualmono.bat to SoX directory, usage:
      rem     cd directoryWithWavFiles
      rem     c:\AUDIO\UT\SOX\finddualmono [flac]
      rem
      rem Careful closing parentheses in REM comments within FOR/IF using parentheses as well
      rem   they must be escaped: ^)
      rem
      set count1=0& rem counter of found stereo files with mono content
      set count2=0& rem counter of generated stereo to mono files
      verify other 2>nul & REM sets errorlevel to 1, check if enableextensions works
      setlocal enableextensions enabledelayedexpansion & REM to get var value at exec with !varNm!
      if %errorlevel%==1 ( echo ERROR newer cmd.exe is needed & goto :fin )
      rem goto conversionTest rem (for debuging)
      set _ext=.wav& rem Default extension
      if /i .%1==.flac set _ext=.flac& rem %1 first parameter flac
      echo.
      echo --------[ Analysing: ]--------
      if exist "!_tempFoundFileList!" del "!_tempFoundFileList!" 1>nul 2>nul
      for %%F IN ("*!_ext!") do (
        set _file=%%F
        echo Analysing: !_file!
        set _command="!_soxExe!" "!_file!" -n remix 1,2i stat 2^>^&1
        rem echo COMMAND: !_command!
        rem https://stackoverflow.com/questions/62972917/batch-script-usage-of-for-f-with-spaces-in-command-and-usebackq
        for /f "tokens=1-3* delims=: " %%A in ('^"!_command!^"') do (
          rem echo --%%A--%%B--%%C--
          if /i "%%A"=="RMS" if /i "%%B"=="amplitude" (
            set value=%%C
            echo RMS:!value!
            rem -20dB each 0 after decimal point, ie: "0.00" will detect: L-R ^< -40dB
            if "!value:~0,4!"=="0.00" (
              set /a count1=!count1!+1
              echo *!count1! Mono Found
              echo .
              echo !_file! >> "!_tempFoundFileList!"
            )
          )
        )
      )
      echo .
      if !count1!==0 ( echo No mono audio found & goto :fin )
      echo --------[ !count1! dual mono files ( Left-Right ^< -40dB ^) ]--------
      type "!_tempFoundFileList!"
      echo -----------------------------
      echo Convert !count1! files to mono?
      echo -----------------------------
      choice
      rem echo %ERRORLEVEL%
      IF %ERRORLEVEL% EQU 2 goto :fin
      :conversionTest
      echo.
      rem TYPE command is needed in case temporaryFile has spaces in it's path
      for /F "tokens=*" %%L in ('^"type "!_tempFoundFileList!"^"') do (
        echo %%L
        set _stereoFile=%%~fL
        rem %%~dpnL drive,path,name no extension
        set _monoOutFile=%%~dpnL_mono!_ext!
        if exist "!_monoOutFile!" ( echo Already done: !_monoOutFile!
        ) else (
          rem --no-clobber asks before overwriting, not needed by previous IF
          set _command="!_soxExe!" --no-clobber "!_stereoFile!" "!_monoOutFile!" remix 1
          !_command!
          if not exist "!_monoOutFile!" ( echo *** Couldn't generate: !_monoOutFile!
          ) else (
            set /a count2=!count2!+1
            echo * Created !count2! Mono: !_monoOutFile!
            rem possible stereo file rename ¿or delete?
            set _newStereoFileName=%%~dpnL_stereo%%~xL
            rem echo Stereo: !_newStereoFileName!
            rem ren "!_stereoFile!" "!_newStereoFileName!"
          )
        )
      )
      rem On REM, exclamation must be double escaped to show Count variables
      echo
      echo ------------------------------------------------
      echo Finished^^! Found !count1! mono, !count2! converted
      echo Send all the money to kamilbaranski.com
      echo ------------------------------------------------
      :fin
      rem Remove temporary file list
      if exist "!_tempFoundFileList!" del "!_tempFoundFileList!" 1>nul 2>nul
      pause
      
       

      Last edit: isidroco 2022-09-14
  • Mans Rullgard

    Mans Rullgard - 2020-08-12
    • status: open --> closed
    • Group: -->
     

Log in to post a comment.