Amount of digital voice broadcasting

Keywords: C# less

Recently, we are working on a voice broadcasting function, which started with Speech. It feels good. It can be transferred directly from text to voice, and then broadcasted. But after testing the machine, we found that English was playing. After searching, we found that voice support was needed. There may also be some systems where the entire voice function may be castrated, so we have to think of other ways.

Another way of thinking: In fact, only the number changes, other voice and text are generally fixed. Then we can record 0-9 as 10 wav files separately, and then divide the amount into char s, playing the corresponding files is OK! Do as you say:

First, record the required files, and the specific implementation code is as follows:

Task.Run(() =>
            {
                decimal payAmt = 123.56M;
                string payString = payAmt.ToString("F2");
                //Play fixed prefix voice "Dragon Payment Receipt"
                using (SoundPlayer simpleSound = new SoundPlayer($"{Application.StartupPath}\\auido\\ccb.wav"))
                {
                    simpleSound.Play();
                    //Here we need to wait for the length of the voice file.
                    System.Threading.Thread.Sleep(1450);
                    foreach (var item in payString.ToArray())
                    {
                        //Play specific numbers
                        simpleSound.SoundLocation = $"{Application.StartupPath}\\auido\\{item}.wav";
                        simpleSound.Play();
                        System.Threading.Thread.Sleep(400);
                    }
                    //Playback element
                    simpleSound.SoundLocation = $"{Application.StartupPath}\\auido\\yuan.wav";
                    simpleSound.Play();
                }
            });

It looks like the function has been implemented, but it sounds a bit dull, but let's do it first. Submitted to the test, after the test came to say that the voice how there is no unit ah, can add units? HMM.!!! Sure.

So began to realize digital broadcasting plus units, first to find a section of the functional code to refer to large characters, in fact, the logic is similar, and then slightly modified on OK, the idea is the same, first recorded voice files, need to increase the unit voice files: 1 million yuan

Next, turn the number Tostring("F2") that needs to be converted. Don't switch to N2 here, otherwise there will be comma-separated. My design here only supports broadcasting of less than 100 million yuan.

Code directly

public void PayAuido(decimal payMoney)
        { 
            if (payMoney >= 100000000 || payMoney == 0)
            {
                return;
            }
            Task.Run(() =>
            {
                List<string> payAuidoUrls = new List<string>();
                string payString = payMoney.ToString("F2");
                //If less than 11 bits, then zero
                if (payString.Length < 11)
                {
                    payString = payString.PadLeft(11, '0');
                }
                string payWan = payString.Substring(0, 4);//Obtain'ten thousand'unit
                string payYuan = payString.Substring(4, 4);//Obtain'element'unit
                string payNum = payString.Substring(9, 2);//Get the decimal part

                //Processing ten thousand
                List<string> payAuidoUrlsWan = Convert(payWan);
                if (payAuidoUrlsWan.Count > 0)
                { 
                    payAuidoUrlsWan.Add($"{Application.StartupPath}\\auido\\wan.wav");
                }

                //Processing element
                List<string> payAuidoUrlsYuan = Convert(payYuan);

                //Is the first character of a meta-character a'Zero'
                if (payAuidoUrlsWan.Count > 0 && payAuidoUrlsYuan.Count > 0 && payString[4] == '0')
                {
                    payAuidoUrlsWan.Add($"{Application.StartupPath}\\auido\\0.wav");
                }
                payAuidoUrls.AddRange(payAuidoUrlsWan);
                payAuidoUrls.AddRange(payAuidoUrlsYuan);

                //Processing decimal digits
                if (payNum != "00")
                {
                    //If only decimal, add'Zero'
                    if (payAuidoUrls.Count == 0)
                    {
                        payAuidoUrls.Add($"{Application.StartupPath}\\auido\\0.wav");
                    }
                    payAuidoUrls.Add($"{Application.StartupPath}\\auido\\..wav");
                    payAuidoUrls.Add($"{Application.StartupPath}\\auido\\{payNum[0]}.wav");
                    //If the last one is'Zero',No need to broadcast
                    if (payNum[1] != '0')
                    {
                        payAuidoUrls.Add($"{Application.StartupPath}\\auido\\{payNum[1]}.wav");
                    }
                }

                //Additive element
                payAuidoUrls.Add($"{Application.StartupPath}\\auido\\yuan.wav");
                //Play Dragon Payment Receipt
                using (SoundPlayer simpleSound = new SoundPlayer($"{Application.StartupPath}\\auido\\ccb.wav"))
                { 
                    simpleSound.Play();
                    System.Threading.Thread.Sleep(1450); 
                    foreach (string payAuidoUrl in payAuidoUrls)
                    {
                        //Play each number
                        simpleSound.SoundLocation = payAuidoUrl; 
                        simpleSound.Play();
                        System.Threading.Thread.Sleep(400); 
                    }
                }
            });
        }

        /// <summary>
        /// Conversion every four digits
        /// </summary>
        /// <param name="payValue">Current conversion amount</param>
        /// <returns></returns>
        public List<string> Convert(string payValue)
        {
            List<string> payAuidos = new List<string>();
            //If it's four zeros, you don't need to broadcast.
            if (payValue == "0000")
            {
                return payAuidos;
            }

            char tmp0 = payValue[0];
            char tmp1 = payValue[1];
            char tmp2 = payValue[2];
            char tmp3 = payValue[3];
            //Thousand bit
            if (tmp0 != '0')
            {
                payAuidos.Add($"{Application.StartupPath}\\auido\\{tmp0}.wav");
                payAuidos.Add($"{Application.StartupPath}\\auido\\qian.wav");
            }
            else
            {
                payAuidos.Add($"{Application.StartupPath}\\auido\\0.wav");
            }
            //Hundred bit
            if (tmp1 != '0')
            {
                payAuidos.Add($"{Application.StartupPath}\\auido\\{tmp1}.wav");
                payAuidos.Add($"{Application.StartupPath}\\auido\\bai.wav");
            }
            else
            {
                //Add only one zero
                if (tmp0 != '0')
                {
                    payAuidos.Add($"{Application.StartupPath}\\auido\\0.wav");
                }
            }
            //Ten place
            if (tmp2 != '0')
            {
                payAuidos.Add($"{Application.StartupPath}\\auido\\{tmp2}.wav");
                payAuidos.Add($"{Application.StartupPath}\\auido\\shi.wav");
            }
            else
            {
                //Add only one zero
                if (tmp1 != '0')
                {
                    payAuidos.Add($"{Application.StartupPath}\\auido\\0.wav");
                }
            }
            //Bit
            if (tmp3 != '0')
            {
                payAuidos.Add($"{Application.StartupPath}\\auido\\{tmp3}.wav");
            }
            //If the first audio is zero, remove it
            if (payAuidos[0].Contains("0.wav"))
            {
                payAuidos.RemoveAt(0);
            }
            //If the last audio is zero, remove it
            if (payAuidos[payAuidos.Count - 1].Contains("0.wav"))
            {
                payAuidos.RemoveAt(payAuidos.Count - 1);
            }
            return payAuidos; 
        }

Input digits can be converted into voice broadcasting. In fact, the idea is very simple, that is, the voice files corresponding to each digit and unit can be played continuously.

Posted by matchew on Mon, 16 Sep 2019 19:50:57 -0700