Search code examples
makefilegnu-make

How to get make to process multiple audio files


I have a part of my make file which does speech to text conversion. It works great on every mp3 file in the directory.

%.txt : %.mp3
    $(WS) $(FLAGS) ./$<

But the software would also just as happily work with .wav .m4a files etc. I spend a lot of time converting things into mp3. Is there any way to process lots of audio files the same way? Something like below (which doesn't work)

%.txt : %.mp3 %.wav %.m4a 
    $(WS) $(FLAGS) ./$<

Post of complete modified Make file

.SUFFIXES : .txt .m4a .mp3 .wav
SOURCES := $(wildcard  *.m4a)
TEXT := $(wildcard  *.txt)
OBJ := $(subst m4a,txt,$(SOURCES))
WS = whisper

FLAGS = --device cuda:1 --model small 
.PHONY: test clean imported

define template =
%.txt: %.$(1)
        $(WS) $(FLAGS) $$<
endef
$(foreach extension,mp3 wav m4a,$(eval $(call template,$(extension))))

BIN := $(wildcard *.mp3 *.wav *.m4a)
TXT := $(addsuffix .txt,$(basename $(BIN)))

all: $(TXT)


clean :
    rm *~ *.json  *.srt  *.tsv  *.vtt 

split: $(TEXT)
    awk -v RS="[.]" -v ORS=".\n" 'NR%70==1 {file = sprintf("./tmp/$(patsubst %.txt,%, $< )%04d.txt", NR)} {print > file}' $< 

imported:
    mv odt/*.odt odt/imported/

test: $(SOURCES)
    @echo $(OBJ)
    @echo PREREQUISITES: $^

Solution

  • Why use a makefile, when plain old shell can do the job?

     WS=ws
     FLAGS="-foo -bar"
     for file in *.mp3 *.wav *.m4a; do
        $WS $FLAGS $file
     done
    

    Any particular reason you want to go the make way? If it's because you don't want to convert files already converted, you could test the existence of the corresponding txt file.

    If you don't want to repeatedly write the same recipe in a makefile because you have a large list of file extensions, you can use a parameterized template recipe and the eval function as follows:

    WS    := @echo ws
    FLAGS := --foo --bar
    
    define template =
    %.txt: %.$(1)
            $(WS) $(FLAGS) $$<
    endef
    $(foreach extension,mp3 wav m4a,$(eval $(call template,$(extension))))
    
    BIN := $(wildcard *.mp3 *.wav *.m4a)
    TXT := $(addsuffix .txt,$(basename $(BIN)))
    
    all: $(TXT)