How to append .wav format sounds to each other

一世执手 提交于 2019-12-12 15:42:33

问题


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). I have done the same program in java but want to transfer it in C#, but i couldn't understand how to append different sounds to each other.For example, when my program wants to say a word "general" it divides the word in parts like this "ge"-first "ne"-second "ra"-second "l"-second (these first and second mean that the part is on the first part of the word, i did so because ge-first and ge-second have different intonations) so, i have recorded these parts in .wav format and want the program 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 to each-other. 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 used the same code in my java program(in java syntax, of course), but i got to the problem when trying to work with the .wav files, i don't know which method to use and how to stick these sounds. this is how i did it on java.

public AudioInputStream find(String s) throws UnsupportedAudioFileException, IOException {
         return AudioSystem.getAudioInputStream(
           new File("C:/Users/Vato/Desktop/Programing/sintezatori/alphabet/"+s+".wav"));
 }
 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;
 }

now i want to do almost the same thing on C#. i looked at this page http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx but couldn't figure out what to do, so please help me :) what method should i use and how?


回答1:


here are great links to help:

  • Concatenating Wave Files Using C# 2005
  • C# WAV file class, audio mixing, and some light audio manipulation

and maybe this: Merge WAV files in C#



来源:https://stackoverflow.com/questions/11501160/how-to-append-wav-format-sounds-to-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!