1

I am studying C# by myself by reading some books and watching some tutorials. So, i decided to make a small project at the same time, to get more experience and harden my knowledge. I am trying to create a text to speech program in Georgian(my language) but, i couldn't understand how to append different sounds to each other. For example, when my program wants to say "language" it will divide the word to "la" "n" "gu" "a" "ge" so, i have recorded these parts and want to append them and create a word. I looked for classes on MSDN.COM and found SoundPlayer but, i couldn't figure out how to append the sounds of WAV format. i want to add one sound to another and play a new one, for example i have sound that says "aaa" and the other says "bbbb" i want to get a sound that says "aaabbbb".

To divide words i created an arraylist and used this code.

public ArrayList divide(String s)  //დაყოფა და arraylist-ში გადანაწილება
    {
        ArrayList a = new ArrayList();
        int i = 0;
        while (i < s.Length)
        {
            if (s[i] == ',' || s[i] == ' ' || s[i] == '.')
            {
                a.Add(s.Substring(i, i + 1));
                i++;
                continue;
            }
            if (consonant(s[i]) && (i + 1) != s.Length && sonant(s[i + 1]))
            {
                if (isFirstSonant(s, i))
                    a.Add(s.Substring(i, i + 2) + "_FIRST");
                else
                    a.Add(s.Substring(i, i + 2) + "_SECOND");
                i += 2;
                continue;
            }
            if (sonant(s[i]) && ((i + 1) < s.Length && sonant(s[i]) || i == (s.Length - 1)))
            {
                if (isFirstSonant(s, i))
                    a.Add(s.Substring(i, i + 1) + "_FIRST");
                else
                    a.Add(s.Substring(i, i + 1) + "_SECOND");
                i++;
                continue;
            }
            if (consonant(s[i]) && ((i + 1) < s.Length && consonant(s[i]) || i == (s.Length - 1)))
            {
                a.Add(s.Substring(i, i + 1) + "_SECOND");
                i++;
                continue;
            }
        }
        return a;
    }

I have made this program on java and want to do the same on C# so this is my code on java. this is how i appended the sounds after, putting them in arraylist.

public AudioInputStream append(AudioInputStream main, String s) throws UnsupportedAudioFileException, IOException {
     return new AudioInputStream(
             new SequenceInputStream(main, find(s)),     
             main.getFormat(), 
             main.getFrameLength() + find(s).getFrameLength());
 }
 private String s;
 public void Process() {
    try {
        AudioInputStream main = AudioSystem.getAudioInputStream(new File("C:/Users/Vato/Desktop/Programing/sintezatori/alphabet/blank.wav"));
        ArrayList<String> aa = divide(s);
        for(int ii=0;ii<aa.size();ii++) {
            main=append(main, aa.get(ii));
            System.out.println(aa.get(ii));
        }
        AudioSystem.write(main, AudioFileFormat.Type.WAVE, new File("C:/Users/Vato/Desktop/Programing/sintezatori/alphabet/result.wav"));
        result=main;
        AudioInputStream result1 = AudioSystem.getAudioInputStream(new File("C:/Users/Vato/Desktop/Programing/sintezatori/alphabet/result.wav")); 
        DataLine.Info info =
            new DataLine.Info(Clip.class,
                    result1.getFormat());
        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open(result1);
        clip.start();
        } catch (Exception e) {
          e.printStackTrace();
        }
   }
 private AudioInputStream result;
 public AudioInputStream getResult() {
     return result;
 }

Which method or class should i use from these http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx ? How can i do the same in C#?

vato
  • 141
  • 2
  • 11
  • I created a new question to make people understand what i really wanted. sorry for this bad question, i wanted to delete it but couldn't . if u want to see this question in better way just go to this link http://stackoverflow.com/questions/11501160/how-to-append-wav-format-sounds-to-each-other-in-c-sharp. thanks :) – vato Jul 16 '12 at 09:10

3 Answers3

2

If you don't want to use an existing SDK you could do something like the following:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Media;
using System.Text;

namespace ConsoleApplication3
{
    public class SpeechClass
    {

        private Dictionary<char, string> _letterToFileMapping = new Dictionary<char, string>();
        private string _basePath = "\\soundfiles";


        public SpeechClass()
        {
            PopulateMappings();
        }


        private void PopulateMappings()
        {
            _letterToFileMapping.Add('a', "asound.wav");
            _letterToFileMapping.Add('b', "bsound.wav");
            _letterToFileMapping.Add('c', "csound.wav");
            _letterToFileMapping.Add('d', "dsound.wav");
        }

        private void SayWord(string word)
        {
            var chars = word.ToCharArray();

            List<string> filestosay = new List<string>();

            foreach (var c in chars)
            {
                string sound;
                if(_letterToFileMapping.TryGetValue(c, out sound))
                {
                    filestosay.Add(sound);
                }
            }

            foreach (string s in filestosay)
            {
                SoundPlayer p = new SoundPlayer();
                p.SoundLocation = Path.Combine(_basePath, s);
                p.Play();

            }
        }
    }
}
tsells
  • 2,751
  • 1
  • 18
  • 20
  • Note this isn't optimized. You could move the sound player outside of the foreach loop so you wouldn't incur the cost of initializing a new object every time. – tsells Jul 15 '12 at 17:19
  • :( i got a problem because "_letterToFileMapping.Add('a', "asound.wav");" this is only about letters and when im creating words im creating it with some parts for example general its "ge" "ne" "ra" "l" – vato Jul 15 '12 at 17:34
  • You can have two dictionaries - one for chars and one for strings ( whole words) - then pass in a string to a method to return the correct one based on string length (length of 1 = char, length > 1 lookup the whole word. – tsells Jul 15 '12 at 21:03
  • :( the whole word is bad, because i will have to create enormous dictionary. the main point is that i will have a small dictionary with only small parts "ba"-First or "ba"-Second- this second and first means in which part of word is this "ba" because intonation changes from one word to another. can u just tell me like how to append "ba.wav" to like "l.wav" and append to "l.wav" to create a word "ball". i only need the appending or adding method. thx – vato Jul 16 '12 at 06:53
1

The AT&T Text-To-Speech SDK is remarkable. You can do custom dictionaries and sounds. http://www.wizzardsoftware.com/text-to-speech-tts.php

Danny G
  • 3,660
  • 4
  • 38
  • 50
1

Sound player should work:

using System.Media;

InitializeComponent();
Soundplayer MySounds = new SoundPlayer(@"C:\example.wav);
MySounds.Play();
qwesr
  • 59
  • 1
  • 7
  • yes i did that but, i want to add one sound to another and play a new one, for example i have sound that says "aaa" and the other says "bbbb" i want to get a sound that says "aaabbbb" – vato Jul 15 '12 at 17:09