简体   繁体   English

如何在C#中添加声音

[英]How to append sounds in C#

I am studying C# by myself by reading some books and watching some tutorials. 我通过阅读一些书籍和观看一些教程来自学C#。 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. 例如,当我的程序想要说“语言”时,它会将单词划分为“la”“n”“gu”“a”“ge”,因此,我记录了这些部分并想要附加它们并创建一个单词。 I looked for classes on MSDN.COM and found SoundPlayer but, i couldn't figure out how to append the sounds of WAV format. 我在MSDN.COM上找了一些课,发现了SoundPlayer但是,我无法弄清楚如何附加WAV格式的声音。 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". 我想将一个声音添加到另一个并播放一个新声音,例如我的声音是“aaa”而另一个声称“bbbb”我希望听到“aaabbbb”的声音。

To divide words i created an arraylist and used this code. 为了划分单词,我创建了一个arraylist并使用了这段代码。

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. 我已经在java上创建了这个程序,并希望在C#上做同样的事情,所以这是我在java上的代码。 this is how i appended the sounds after, putting them in arraylist. 这就是我如何追加声音,将它们放入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 ? 我应该从这些http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx使用哪种方法或类? How can i do the same in C#? 我怎样才能在C#中做同样的事情?

If you don't want to use an existing SDK you could do something like the following: 如果您不想使用现有SDK,可以执行以下操作:

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();

            }
        }
    }
}

The AT&T Text-To-Speech SDK is remarkable. AT&T文字转语音SDK非常出色。 You can do custom dictionaries and sounds. 你可以做自定义词典和声音。 http://www.wizzardsoftware.com/text-to-speech-tts.php http://www.wizzardsoftware.com/text-to-speech-tts.php

Sound player should work: 声音播放器应该工作:

using System.Media;

InitializeComponent();
Soundplayer MySounds = new SoundPlayer(@"C:\example.wav);
MySounds.Play();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM