Click here to Skip to main content
15,881,709 members
Articles / Web Development / ASP.NET

Silverlight Pronunciation Test

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
29 Apr 2012CPOL11 min read 43.7K   1.7K   15  
How to create a pronunciation test tool using Silverlight and Python
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAudio.Utils;
using NAudio.Wave;

namespace PitchContour
{
    public class StereoToMono
    {
        string sourceFile = string.Empty;
        string targetFile = string.Empty;

        public StereoToMono(string sourceFile, string targetFile)
        {
            this.sourceFile = sourceFile;
            this.targetFile = targetFile;
        }

        public void Convert()
        {
            using (WaveFileReader reader = new WaveFileReader(this.sourceFile))
            {
                var s2m = new StereoToMonoProvider16(reader);

                byte[] data = new byte[reader.Length];
                byte[] data2 = new byte[reader.Length];
                var bytesRead = ReadMono(reader, data2, 0, data.Length);

                using (WaveFileWriter writer = new WaveFileWriter(this.targetFile, s2m.WaveFormat))
                {
                    writer.Write(data2, 0, bytesRead);
                }
            }
        }

        int ReadMono(WaveFileReader reader, byte[] buffer, int offset, int count)
        {
            IWaveProvider sourceProvider = reader;
            WaveFormat outputFormat = new WaveFormat(sourceProvider.WaveFormat.SampleRate, 1);
            byte[] sourceBuffer = new byte[reader.Length];

            //var bytesRead = reader.Read(sourceBuffer, 0, sourceBuffer.Length);

            int sourceBytesRequired = count * 2;
            sourceBuffer = BufferHelpers.Ensure(sourceBuffer, sourceBytesRequired);
            WaveBuffer sourceWaveBuffer = new WaveBuffer(sourceBuffer);
            WaveBuffer destWaveBuffer = new WaveBuffer(buffer);

            int sourceBytesRead = sourceProvider.Read(sourceBuffer, 0, sourceBytesRequired);
            int samplesRead = sourceBytesRead / 2;
            int destOffset = offset / 2;
            for (int sample = 0; sample < samplesRead; sample += 2)
            {
                short left = sourceWaveBuffer.ShortBuffer[sample];
                short right = sourceWaveBuffer.ShortBuffer[sample + 1];
                float outSample = (left * 1.0f) + (right * 1.0f);
                // hard limiting
                if (outSample > Int16.MaxValue) outSample = Int16.MaxValue;
                if (outSample < Int16.MinValue) outSample = Int16.MinValue;

                destWaveBuffer.ShortBuffer[destOffset++] = (short)outSample;
            }

            buffer = destWaveBuffer.ByteBuffer;

            return sourceBytesRead / 2;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions