Play sound

hi
i want to produce digit sounds.for ex: if my input text is four five six then code should able to
produce sounds corresponding to digits which are stored in some directory as four.wav,five.wav etc.Please help me

(This could be homework.)

I am not sure i am understanding you correctly but I assume you want the sounds to be like morse code...

This will generate a single tone.

http://www.unix.com/shell-programming-scripting/213525-crude-1khz-audio-sinewave-generator-demo.html

It is up to you to work out morse code with it which is not difficult...

Alternatively if it as voice you want the look at "cat" and use it to record raw voice data directly to disk, as <number>.raw using a laptop's internal mic for ease of use...

This assumes that you have /dev/dsp as a device.

Not sure why you need <number>.wav but if that is a requirement then look at SOX for doing all of the above...

1 Like

I need a simple text to speech converter.I have already have sounds corresponding to digits(0 to 9) in wav format.So when my input is for eg: one two three,then i should able to play one.wav two,wav three.wav.I hope you understood my problem

I suppose a brutally simple way would just be a loop:

tr '[A-Z]' '[a-z]' < inputfile | while read LINE
do
        STR=""
        set -- $LINE
        while [ "$#" -gt 0 ]
        do
                [ -f "wavdir/$1.wav" ] && STR="$STR wavdir/$1.wav"
                shift
        done

        [ ! -z "$STR" ] && play $STR
done

It converts all uppercase into lowercase, then reads every line, splits words on whitespace into $1 $2 ..., loops over $1...$N, and for every word matching a file inside wavdir/, adds it to a list of files to be played then plays it.

You need sox's play command for this...

1 Like

Hi sreejithalokkan...

Makes much more sense now... ;o)

I will assume that you will need a a dedicated shell Audio I/O app for platform independence.
So I will suggest SOX...

I have to assume also that this is some _homework_ style project, but not necessarily so.

OK.

Think of your problem...

Keep it simple...

I am not going to give you the code directly as you learn nothing about what is going on...

1) Generate a function, let us call it "say_it()"
2) inside the function call SOX somehow to play your *.wav file. Something like this:-

say_it()
{
        /full/absolute/path/to/sox <your player options> somefile.wav
}

3) Use if - then - fi to call it something like this:-

number="one"
if [ "$number" == "one" ]
then
        # Your new function...
        say_it
fi

All "case"s can be catered for this way and this is its simplest and easiest to understand form, albeit slow...

Next try and make the function universal by making "somefile.wav" a variable...
1 Like

Quite the same solution as Corona688; uses aplay to play wav files:

#!/bin/sh

while read line; do
	set -- $line
	while [ "$1" ]; do
		echo aplay "$1.wav" # remove echo to actually let aplay play the wav file
		shift
	done
done

exit 0

Example usage: ~/unix.com$ echo input_file | sh the_above_script.sh

~/unix.com$ cat input_file
one four five
two
three

~/unix.com$ cat input_file | sh the_above_script.sh 
aplay one.wav
aplay four.wav
aplay five.wav
aplay two.wav
aplay three.wav

Of course, remove the echo that prevent the aplay to do its job :stuck_out_tongue:

while [ "$1" ]

I don't think this does quite what you think it does. To check if a string is empty:

while [ ! -z "$1" ]

Hi tukuyomi...

IIRC aplay is part of ALSA...
Never make assumptions unless stating them.
Macbook Pro, 13 inch, OSX 10.7.5...

Last login: Thu Mar 28 21:02:56 on ttys000
Barrys-MacBook-Pro:~ barrywalker$ aplay
-bash: aplay: command not found
Barrys-MacBook-Pro:~ barrywalker$ 

Alternatively...

Last login: Thu Mar 28 21:04:40 on ttys000
Barrys-MacBook-Pro:~ barrywalker$ afplay
Usage:
afplay [option...] audio_file

Options: (may appear before or after arguments)
  {-v | --volume} VOLUME
    set the volume for playback of the file
  {-h | --help}
    print help
  { --leaks}
    run leaks analysis
  {-t | --time} TIME
    play for TIME seconds
  {-r | --rate} RATE
    play at playback rate
  {-q | --rQuality} QUALITY
    set the quality used for rate-scaled playback (default is 0 - low quality, 1 - high quality)
  {-d | --debug}
    debug print output
Barrys-MacBook-Pro:~ barrywalker$ 

<WINK>

[quote=tukuyomi;302787121]
Quite the same solution as Corona688; uses aplay to play wav files:

#!/bin/sh

while read line; do
	set -- $line
	while [ "$1" ]; do
		echo aplay "$1.wav" # remove echo to actually let aplay play the wav file
		shift
	done
done

exit 0

Example usage: ~/unix.com$ echo input_file | sh the_above_script.sh

~/unix.com$ cat input_file
one four five
two
three

~/unix.com$ cat input_file | sh the_above_script.sh 
aplay one.wav
aplay four.wav
aplay five.wav
aplay two.wav
aplay three.wavOf course, remove the echo that prevent the aplay to do its job :p[/QUOTE

this code is very much useful for me,thanks