Exemplo n.º 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));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Post([FromForm] string phoneNumber)
        {
            //1) split number to to get 10 numbers in array
            var numsInPhone = phoneNumber.ToCharArray();
            //var chunkAudioFiles = new AudioFileReader[10];
            var chunkAudioStereo = new ISampleProvider[10];
            var backGroundMerger = new ISampleProvider[2];

            string contentRootPath = _env.ContentRootPath;
            string webRootPath     = _env.WebRootPath;

            byte[]         buffer         = new byte[1024];
            WaveFileWriter waveFileWriter = null;
            var            timeRange      = new double[10];
            string         outputFile     = webRootPath + "\\app\\AudioFiles\\output.wav";

            //string outputFile = contentRootPath + "\\output.wav";
            try
            {
                var counter = 0;
                foreach (var num in numsInPhone)
                {
                    //if (counter > 5)
                    // continue;

                    WaveFileReader reader = new WaveFileReader(webRootPath + "\\app\\AudioFiles\\file_" + num + ".wav");
                    if (waveFileWriter == null)
                    {
                        // first time in create new Writer
                        waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);
                    }
                    else
                    {
                        if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat))
                        {
                            var audio = new WdlResamplingSampleProvider(new AudioFileReader(webRootPath + "\\app\\AudioFiles\\file_" + num + ".wav"), 44100);
                            reader = (WaveFileReader)audio.ToStereo();
                        }
                    }
                    timeRange[counter] = reader.TotalTime.TotalSeconds;
                    int read;
                    if (waveFileWriter.WaveFormat.BitsPerSample == 24)
                    {
                        while ((read = reader.Read(buffer, 0, buffer.Length - (buffer.Length % waveFileWriter.WaveFormat.BlockAlign))) > 0)
                        {
                            waveFileWriter.Write(buffer, 0, read);
                        }
                    }
                    else
                    {
                        while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            waveFileWriter.Write(buffer, 0, read);
                        }
                    }

                    counter++;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (waveFileWriter != null)
                {
                    waveFileWriter.Dispose();
                }
            }

            //var sourceFiles = new List<string>();

            //foreach (var num in numsInPhone)
            //{
            //    sourceFiles.Add(webRootPath + "\\app\\AudioFiles\\file_" + num + ".wav");
            //}

            //Concatenate(outputFile, sourceFiles);

            backGroundMerger[0] = new WdlResamplingSampleProvider(new AudioFileReader(webRootPath + "\\app\\AudioFiles\\output.wav"), 44100).ToStereo();
            backGroundMerger[1] = new WdlResamplingSampleProvider(new AudioFileReader(webRootPath + "\\app\\AudioFiles\\harmony.wav"), 44100).ToStereo();


            var finalAudioName = "YourTone.wav";
            // 3) Merge Audios
            var mixer = new MixingSampleProvider(backGroundMerger);

            WaveFileWriter.CreateWaveFile16(finalAudioName, mixer);


            //TODO : Uncomment below and test after 16 bit audio are received
            ConvertWavToMp3(finalAudioName, "mixed.mp3");


            //TODO :  Use generated mp3 after 16 bit audio
            // var fileStream = await System.IO.File.ReadAllBytesAsync(finalAudioName);

            //// var fileStream = await System.IO.File.ReadAllBytesAsync("mp3_sample.mp3");

            // if (fileStream == null)
            //     return NotFound(); // returns a NotFoundResult with Status404NotFound response.

            // return new FileContentResult(fileStream, "audio/mpeg")
            // {
            //     FileDownloadName = outputFile
            // };


            var memory = new MemoryStream();

            using (var stream = new FileStream("mixed.mp3", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                await stream.CopyToAsync(memory);
            }
            memory.Position = 0;

            return(File(memory, "audio/mpeg", $"mp3_sample.mp3", true));
        }