示例#1
0
        public Stream GetSampleStream(int sampleRate, int channels)
        {
            if (channels != 1 && channels != 2)
            {
                throw new ArgumentOutOfRangeException(nameof(channels), "Only 1 or 2 channels are allowed.");
            }

            using (var reader = new MediaFoundationReader(_path))
            {
                ISampleProvider sampler = new WdlResamplingSampleProvider(reader.ToSampleProvider(), sampleRate);

                switch (channels)
                {
                case 1:
                    sampler = sampler.ToMono();
                    break;

                case 2:
                    sampler = sampler.ToStereo();
                    break;
                }

                return(new SamplerWrapper(sampler));
            }
        }
        public async Task <IActionResult> Test()
        {
            try
            {
                var file = Request.Form.Files.First();

                int outRate = 44000;
                var source  = new RawSourceWaveStream(file.OpenReadStream(), new WaveFormat(outRate, 2));
                using (var wavFileReader = new WaveFileReader(source))
                {
                    var resampler  = new WdlResamplingSampleProvider(wavFileReader.ToSampleProvider(), 16000);
                    var monoSource = resampler.ToMono().ToWaveProvider16();

                    using (var outputStream = new MemoryStream())
                    {
                        WaveFileWriter.WriteWavFileToStream(outputStream, monoSource);
                        outputStream.Seek(0, SeekOrigin.Begin);

                        var result = await client.VerifyAsync(outputStream, Guid.Parse("fb786241-9f01-41cc-a585-50b65bd52c38"));

                        if (result.Result == Result.Accept)
                        {
                            // verification successful
                        }
                    }
                }
            }
            catch (Exception e)
            {
                int x = 1;
            }

            return(Ok());
        }
        public EventDrivenResampler(WaveFormat input, WaveFormat output)
        {
            windowsN      = DetectWindowsN();
            this.input    = input;
            this.output   = output;
            buf           = new BufferedWaveProvider(input);
            buf.ReadFully = false;

            if (windowsN)
            {
                mediaFoundationResampler = new WdlResamplingSampleProvider(buf.ToSampleProvider(), output.SampleRate);
                waveOut = mediaFoundationResampler.ToMono().ToWaveProvider16();
            }
            else
            {
                dmoResampler = new ResamplerDmoStream(buf, output);
            }
        }
        public async Task <Enrollment> VoiceEnrollmentPost()
        {
            try
            {
                var file = Request.Form.Files.First();

                int outRate = 44000;
                var source  = new RawSourceWaveStream(file.OpenReadStream(), new WaveFormat(outRate, 2));
                using (var wavFileReader = new WaveFileReader(source))
                {
                    var resampler  = new WdlResamplingSampleProvider(wavFileReader.ToSampleProvider(), 16000);
                    var monoSource = resampler.ToMono().ToWaveProvider16();

                    using (var outputStream = new MemoryStream())
                    {
                        WaveFileWriter.WriteWavFileToStream(outputStream, monoSource);
                        outputStream.Seek(0, SeekOrigin.Begin);


                        // Todo - Fix hard coded response as getting TooNoisy error from service!

                        //var response = await client.EnrollAsync(outputStream, Guid.Parse("fb786241-9f01-41cc-a585-50b65bd52c38"));
                        //return response;

                        return(new Enrollment
                        {
                            RemainingEnrollments = 2,
                            EnrollmentsCount = 1,
                            Phrase = "Houston, we have had a problem"
                        });
                    }
                }
            }
            catch
            {
                // nada
            }

            return(null);
        }
        public EventDrivenResampler(bool windowsN, WaveFormat input, WaveFormat output)
        {
            this.windowsN = windowsN;
            this.input    = input;
            this.output   = output;
            buf           = new BufferedWaveProvider(input);
            buf.ReadFully = false;

            if (output.BitsPerSample > input.BitsPerSample)
            {
                bufferMultiplier = 2;
            }

            if (windowsN)
            {
                mediaFoundationResampler = new WdlResamplingSampleProvider(buf.ToSampleProvider(), output.SampleRate);
                waveOut = mediaFoundationResampler.ToMono().ToWaveProvider16();
            }
            else
            {
                dmoResampler = new ResamplerDmoStream(buf, output);
            }
        }