public async Task Write(Stream fileStream)
        {
            string wavIntermediate = Path.Combine(outputDirectoryProvider.GetOutputDirectory(), Guid.NewGuid() + ".wav");

            try
            {
                {
                    await waveStream.Write(wavIntermediate);

                    await progressReporter.ReportProgress(0.97, JobProgressStatus.InProgress, "Converting WAV to MP3");

                    await using var audioFileReader = new AudioFileReader(wavIntermediate);
                    await using var writer          = new LameMP3FileWriter(fileStream, audioFileReader.WaveFormat, LAMEPreset.ABR_320);
                    await audioFileReader.CopyToAsync(writer);
                } // dispose it before reporting complete
                await progressReporter.ReportProgress(1, JobProgressStatus.Complete, "File created successfully");
            }
            finally
            {
                if (File.Exists(wavIntermediate))
                {
                    try { File.Delete(wavIntermediate); } catch (Exception) { }
                }
            }
        }
        public static async Task <string> WaveToMP3Async(string waveFileName,
                                                         string mp3FileName,
                                                         int bitRate              = 192,
                                                         string title             = "",
                                                         string subtitle          = "",
                                                         string comment           = "",
                                                         string artist            = "",
                                                         string albumArtist       = "",
                                                         string album             = "",
                                                         string year              = "",
                                                         string track             = "",
                                                         string genre             = "",
                                                         byte[] albumArt          = null,
                                                         string[] userDefinedTags = null)
        {
            ID3TagData tag = new ID3TagData
            {
                Title           = title,
                Artist          = artist,
                Album           = album,
                Year            = year,
                Comment         = comment,
                Genre           = LameMP3FileWriter.Genres[36], // 36 is game.  FUll list @ http://ecmc.rochester.edu/ecmc/docs/lame/id3.html
                Subtitle        = subtitle,
                AlbumArt        = albumArt,
                AlbumArtist     = albumArtist,
                Track           = track,
                UserDefinedTags = userDefinedTags
            };

            var reader = new AudioFileReader(waveFileName);

            if (reader.WaveFormat.Channels <= 2)
            {
                using (reader)
                {
                    using (var writer = new LameMP3FileWriter(mp3FileName, reader.WaveFormat, bitRate, tag))
                        await reader.CopyToAsync(writer); //reader.CopyTo(writer);
                }
            }
            else if (reader.WaveFormat.Channels > 2)
            {
                reader.Dispose();
                mp3FileName = string.Empty;
                SplitWav(waveFileName);
                var fileNames = MixSixChannel(waveFileName);
                foreach (var fileName in fileNames)
                {
                    using (reader = new AudioFileReader(fileName))
                    {
                        using (var writer = new LameMP3FileWriter(fileName.Replace(".wav", ".mp3"), reader.WaveFormat, bitRate, tag))
                            await reader.CopyToAsync(writer);
                        mp3FileName += fileName.Replace(".wav", ".mp3") + " ";
                    }
                }
            }

            return($"{mp3FileName} created.\r\n\r\n");
        }
Beispiel #3
0
            public async Task ExecuteConversionAsync()
            {
                Directory.CreateDirectory(
                    new FileInfo(
                        GenerateTargetFilePath()
                        )
                    .Directory
                    .FullName
                    );

                //var albumCoverBytes = await _albumCoverFileInfo
                //    .PipeAsync(async x =>
                //    {
                //        using (var fileStream = File.OpenRead(
                //            _albumCoverFileInfo?.FullName))
                //        {
                //            using (var memoryStream = new MemoryStream())
                //            {
                //                await fileStream.CopyToAsync(memoryStream);
                //                memoryStream.Position = 0;
                //                return memoryStream.ToArray();
                //            }
                //        }
                //    });

                //var id3TagData = this.Pipe(@this =>
                //    {
                //        using (var wmaStream = new NAudio.WindowsMediaFormat.WmaStream(_sourceFile.FullName))
                //        {
                //            return new ID3TagData()
                //            {
                //                // AlbumArt = albumCoverBytes,
                //                Album = wmaStream["WM/AlbumTitle"],
                //                Title = wmaStream["Title"],
                //                Artist = wmaStream["WM/AlbumArtist"],
                //                Track = wmaStream["WM/TrackNumber"],
                //                Year = wmaStream["WM/Year"]
                //            };
                //        }
                //    });


                using (var reader = new AudioFileReader(_sourceFile.FullName))
                {
                    using (var writer = new LameMP3FileWriter(
                               GenerateTargetFilePath(),
                               reader.WaveFormat,
                               LAMEPreset.STANDARD// ,
                               // id3TagData
                               ))
                    {
                        await reader.CopyToAsync(writer);
                    }
                }
            }
Beispiel #4
0
        private async Task ConvertAsync(
            string waveFileName,
            string mp3FileName,
            int bitRate        = 192,
            string title       = "",
            string subtitle    = "",
            string comment     = "",
            string artist      = "",
            string albumArtist = "",
            string album       = "",
            string year        = "",
            string track       = "",
            string genre       = "",
            byte[] albumArt    = null)
        {
            var tag = new ID3TagData
            {
                Title       = title,
                Artist      = artist,
                Album       = album,
                Year        = year,
                Comment     = comment,
                Genre       = genre.Length == 0 ? LameMP3FileWriter.Genres[36] : genre, // 36 is game.  Full list @ http://ecmc.rochester.edu/ecmc/docs/lame/id3.html
                Subtitle    = subtitle,
                AlbumArt    = albumArt,
                AlbumArtist = albumArtist,
                Track       = track
            };
            var reader = new AudioFileReader(waveFileName);

            if (reader.WaveFormat.Channels <= 2)
            {
                using (reader)
                    using (var writer = new LameMP3FileWriter(mp3FileName, reader.WaveFormat, bitRate, tag))
                    {
                        await reader.CopyToAsync(writer);
                    }
                var createMessage = $"{mp3FileName} created";
                _sendMessageEvent.OnSendMessageEvent(new SendMessageEventArgs(createMessage));
                _logger.LogInformation(createMessage);
            }
            else if (reader.WaveFormat.Channels == 4 || reader.WaveFormat.Channels == 6)
            {
                reader.Dispose();
                mp3FileName = string.Empty;
                await Task.Run(() => SplitWav(waveFileName));

                var fileNames = MixSixChannel(waveFileName);
                foreach (var fileName in fileNames)
                {
                    using (reader = new AudioFileReader(fileName))
                    {
                        using (var writer = new LameMP3FileWriter(fileName.Replace(".wav", ".mp3"), reader.WaveFormat, bitRate: bitRate, id3: tag))
                        {
                            await reader.CopyToAsync(writer);
                        }
                        mp3FileName += string.IsNullOrEmpty(mp3FileName) ? fileName.Replace(".wav", ".mp3") + " & " : fileName.Replace(".wav", ".mp3");
                    }
                }
                var createMessage = $"{mp3FileName} created";
                _sendMessageEvent.OnSendMessageEvent(new SendMessageEventArgs(createMessage));
                _logger.LogInformation(createMessage);
            }
            else
            {
                throw new Exception($"Could not convert {mp3FileName.Trim()}: It has {reader.WaveFormat.Channels} channels.");
            }
        }