Exemple #1
0
        private async Task EncodeWaveFileToMediaFile()
        {
            var restrictions = WaveFormat.GetMP3RestrictionCode();

            using (var tempFileStream = _fileSystem.File.OpenRead(_tempOriginalFile))
            {
                tempFileStream.Position = 0;
                using (var tempReader = new WaveFileReader(tempFileStream))
                {
                    tempReader.Position = 0;
                    using (var mediaFileStream = _fileSystem.FileStream.Create(_tempEncodeFile, FileMode.Create,
                                                                               FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        using (var mediaWriter = GetMediaFileWriter(mediaFileStream, WaveFormat))
                        {
                            if (_userSettings.MediaFormat == MediaFormat.Mp3 && restrictions.Any())
                            {
                                await WriteWaveProviderReducerToMP3FileWriter(mediaWriter,
                                                                              GetMp3WaveProvider(tempReader, WaveFormat));
                            }
                            else
                            {
                                await tempReader.CopyToAsync(mediaWriter, 81920, _cancellationTokenSource.Token);
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
 private async Task WriteStreamOutputToFileBasedOnNumberOfChannels()
 {
     using (var reader = new WaveFileReader(_streamOutput))
     {
         await reader.CopyToAsync(_fileWriter);
     }
     //if (_userSettings.MediaFormat == MediaFormat.Mp3 && _waveIn.WaveFormat.Channels > MP3_SUPPORTED_NUMBER_CHANNELS)
     //{
     //    // TODO: #97 NAudio Multi channel pass through
     //    //var waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(_waveIn.WaveFormat.SampleRate, MP3_SUPPORTED_NUMBER_CHANNELS);
     //    ////using (var converter = new WaveFormatConversionStream(waveFormat, reader))
     //    ////{
     //    ////    await converter.CopyToAsync(_fileWriter);
     //    ////}
     //    //using (var r = new RawSourceWaveStream(_streamOutput, waveFormat)) {
     //    //    await r.CopyToAsync(_fileWriter);
     //    //}
     //}
     //else
     //{
     //    using (var reader = new WaveFileReader(_streamOutput))
     //    {
     //        await reader.CopyToAsync(_fileWriter);
     //    }
     //}
 }
Exemple #3
0
        public async Task Resample(string srcFile, string destFile)
        {
            var wavFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(destFile), $"{System.IO.Path.GetFileNameWithoutExtension(destFile)}.wav");

            if (System.IO.File.Exists(wavFile))
            {
                System.IO.File.Delete(wavFile);
            }
            using (var reader = new AudioFileReader(srcFile))
            {
                var resampler = new WdlResamplingSampleProvider(reader, 44100);
                WaveFileWriter.CreateWaveFile16(wavFile, resampler);
            }
            using (var reader = new WaveFileReader(wavFile))
            {
                if (System.IO.File.Exists(destFile))
                {
                    System.IO.File.Delete(destFile);
                }
                using (var writer = new LameMP3FileWriter(destFile, reader.WaveFormat, LAMEPreset.EXTREME))
                {
                    //reader.CopyTo(writer);
                    await reader.CopyToAsync(writer);

                    await writer.FlushAsync();
                }
            }
            System.IO.File.Delete(wavFile);
        }
Exemple #4
0
        /// <summary>
        /// Takes an actual wav file and converts it
        /// </summary>
        /// <param name="wavFile"></param>
        /// <param name="deleteWavAfter"></param>
        /// <returns></returns>
        public static Stream ConvertWavFileToMp3MemoryStream(string wavFile, bool deleteWavAfter)
        {
            var mp3MemoryStream = FilesystemUtils.recyclableMemoryStreamManager.GetStream();

            using (var rdr = new WaveFileReader(wavFile))
                using (var wtr = new LameMP3FileWriter(mp3MemoryStream, rdr.WaveFormat, BitRate))
                {
                    rdr.CopyToAsync(wtr).GetAwaiter().GetResult();
                }
            if (deleteWavAfter)
            {
                File.Delete(wavFile);
            }
            return(mp3MemoryStream);
        }
Exemple #5
0
        private async Task WriteTempWaveToMediaFile()
        {
            var restrictions = _waveIn.WaveFormat.GetMP3RestrictionCode();

            using (var reader = new WaveFileReader(_tempFile))
            {
                reader.Position = 0;
                if (_userSettings.MediaFormat == MediaFormat.Mp3 && restrictions.Any())
                {
                    await WriteWaveProviderReducerToMP3FileWriter(GetMp3WaveProvider(reader, _waveIn.WaveFormat));
                }
                else
                {
                    await reader.CopyToAsync(_fileWriter);
                }
            }
        }
        public async Task PlayAsync(CommandContext commandContext)
        {
            var voiceNext           = commandContext.Client.GetVoiceNext();
            var voiceNextConnection = voiceNext.GetConnection(commandContext.Guild);

            if (voiceNextConnection != null)
            {
                voiceNextConnection.SendSpeaking();
                using (var transmitStream = voiceNextConnection.GetTransmitStream())
                    using (var reader = new WaveFileReader("Flurry.wav"))
                    {
                        await reader.CopyToAsync(transmitStream);
                    }
                voiceNextConnection.SendSpeaking(false);
            }
            else
            {
                await commandContext.RespondAsync("The bot is not connected to any voice channel");
            }
        }
Exemple #7
0
        private async Task UpdateOutputFileBasedOnMediaFormat()
        {
            switch (_userSettings.MediaFormat)
            {
            case MediaFormat.Wav:
                _fileManager.Rename(_currentOutputFile.ToPendingFileString(), _currentOutputFile.ToString());
                return;

            case MediaFormat.Mp3:
                using (var reader = new WaveFileReader(_currentOutputFile.ToPendingFileString()))
                {
                    using (var mp3writer = new LameMP3FileWriter(_currentOutputFile.ToTranscodingToMP3String(), _waveIn.WaveFormat, _userSettings.Bitrate))
                    {
                        await reader.CopyToAsync(mp3writer);

                        if (mp3writer != null)
                        {
                            await mp3writer.FlushAsync();
                        }
                    }
                }

                _fileManager.DeleteFile(_currentOutputFile.ToPendingFileString());
                _fileManager.Rename(_currentOutputFile.ToTranscodingToMP3String(), _currentOutputFile.ToString());

                var mp3TagsInfo = new MediaTags.MP3Tags()
                {
                    Track = _track,
                    OrderNumberInMediaTagEnabled = _userSettings.OrderNumberInMediaTagEnabled,
                    Count       = _userSettings.OrderNumber,
                    CurrentFile = _currentOutputFile.ToString()
                };
                await mp3TagsInfo.SaveMediaTags();

                return;

            default:
                return;
            }
        }