0

I was looking at writing a script to convert the contents of a .txt file to .wav files.

The command that currently does is :

text2wave textFileName.txt -o wavFileName.wav

So, pretty simple command. It looks at what's inside the .txt file and converts it all to a .wav file. But what I would like to do is have just one .txt that contains a number of lines and each of those lines would be converted to a .wav file. For example : line 0, line 1, line 2, ... to be converted into 0.wav, 1.wav, 2.wav, ... etc

Any ideas on how to achieve this? I'm also open to different approaches.

Ashish K
  • 905
  • 10
  • 27
Marco Neves
  • 141
  • 15

1 Answers1

2
#!/bin/bash

i=1
filename=/path/to/text/file

while read -r line
do
    echo $line > somefile.txt
    text2wave somefile.txt -o $i.wav
    i=$((i+1))

done < "$filename"

I think this would do your job of extracting each line and converting into respective .wav file.

Ashish K
  • 905
  • 10
  • 27
  • I have tried making it as simple as possible. You can go through it and ping if there is anything that I can make clear for you :) – Ashish K Mar 30 '17 at 05:41
  • Okay this is really legiable. Only line I don't quite understand is `count=`wc -l $text | awk '{ print $1 }'` and I guess the while loop. – Marco Neves Mar 30 '17 at 05:45
  • From the count variable I am trying to read the number of lines that are present in the text file. But `wc -l` gives you output in format ` ` of which awk gives me only the count. – Ashish K Mar 30 '17 at 05:47
  • Correct me if I'm wrong but you're basically getting the i-th line and writing it to somefile.txt and creating a .wav file and then repeating that process for the other lines. – Marco Neves Mar 30 '17 at 05:48
  • In while loop I check whether the `i` variable which I am incrementing is below or equal to the `count` and at each iteration it extracts one line from the text file, puts it into `somefile.txt` and converts `somefile.txt` to `.wav`. The `>` character over-writes somefile.txt at each iteration with the content of the next line. – Ashish K Mar 30 '17 at 05:50
  • Absolutely. That is the reason I used `>` and not `>>` – Ashish K Mar 30 '17 at 05:50
  • Got it, it's pretty simple and does the job :) Thank you. – Marco Neves Mar 30 '17 at 05:52
  • Okay, I don't know what went wrong but it's sort of working but not really. It creates the correct wave file for the first line, but then from there on it adds all the previous lines and compiles it into the current i.wav – Marco Neves Mar 30 '17 at 06:28
  • @marcoNeves are you sure you are using `>` and not `>>` – Ashish K Mar 30 '17 at 07:05
  • Yes, single operand. – Marco Neves Mar 30 '17 at 07:09
  • let me check me on my system! – Ashish K Mar 30 '17 at 07:14
  • Sure thing, you might need to install a some external packages though. – Marco Neves Mar 30 '17 at 07:16
  • @marcoNeves , I think it's complete and good going now. Actually `head` command is cumulative and takes all the lines `<=` the supplied limit. In this case the `read` command reads each line one by one and stores the value in `$line`. You can futher use this variable `$line` as you wish. Let me know if you have anything else to understand. – Ashish K Mar 30 '17 at 08:58