user1108948
user1108948

Reputation:

Threading doesn't work correctly

I have an application, which is to repeat the numbers what I hear then record the digits what I read. The numbers are 0,1,2,3,4,5,6,7,8,9. I use a for loop to play these numbers with a text to speech skill, which is from a third party.

For the recording part, I have to put it in a separate thread by the third party requirement. To record the voice, the method is likely:

 recordVoiceResource.Record(fileName);

To stop it, use:

 recordVoiceResource.Stop();

Now I find sometimes my recording is 0 second, which means the code perhaps doesn't reach Record line. Sometimes it only has 2 seconds. I believe the thread schedule is wrong.

 private ManualResetEvent terminate = new ManualResetEvent(false);

 PlayTTS("Please repeat the following numbers as you hear them.");

                Thread t = new Thread(() => RecordNumbers());
                t.Start();
                Thread.Sleep(2000);
                terminate.Set();
                terminate.WaitOne();
                PlayNumbers();
                recordVoiceResource.Stop();

The thread method is:

    private void RecordNumbers()
    {
        recordVoiceResource = TelephonyServer.GetVoiceResource();
        recordVoiceResource.MaximumSilence = 1;
        recordVoiceResource.MaximumTime = 30;
        // Start recording what I read from I heard
        recordVoiceResource.Record(fileName);
    }

To playNumbers,

    private void PlayNumbers()
    {
        foreach (var item in numbers)
        {
            try
            {
                vr.PlayTTS(item.ToString()); // will be 0,1,2,...9
                Thread.Sleep(2000);
            }

Upvotes: 0

Views: 118

Answers (2)

Mike Strobel
Mike Strobel

Reputation: 25623

According to your comment, the property MaximumSilence gets or sets the maximum silence in seconds that will be allowed until termination of the next voice function. You are setting it to one second, starting the recording, and then sleeping for two seconds before beginning playback that prompts the user to say something. Do you see the problem here? Assuming the mic doesn't pick up some unrelated speech during that period, the recording will stop before the playback even begins.

Since there is a 2-second gap between number playback, you probably need to set MaximumSilence to several seconds.

That is, of course, assuming your intention was to capture a single recording of the user speaking all the numbers (which is how your code is written). If you want to capture the spoken numbers individually, then you may need to schedule and synchronize separate recordings as each number is played back. You may want to double-check the API to make sure your solution is what you intended.

Upvotes: 2

apomene
apomene

Reputation: 14389

It is very likely your problem is causing due to Thread.Sleep(). Use a timer instead:

System.Timers.Timer Record = new System.Timers.Timer();
Record.Interval = 2000;
Record.Elapsed += new System.Timers.ElapsedEventHandler(Record_Elapsed);

            void Record_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {                   
                 Record.Enabled=false;                  
                 PlayNumbers();
                 recordVoiceResource.Stop();
            }

And set:

Thread t = new Thread(() => RecordNumbers());
              t.Start();
              Record.Enabled=true;

Upvotes: 0

Related Questions