コード例 #1
0
 public static MemoryStream ConvertWavToMp3(Wave32To16Stream wavFile)
 {
     using (var retMs = new MemoryStream())
         using (var wtr = new LameMP3FileWriter(retMs, wavFile.WaveFormat, 128))
         {
             wavFile.CopyTo(wtr);
             return(retMs);
         }
 }
コード例 #2
0
        private void convertToMp3()
        {
            if (IsRecording)
            {
                return;
            }

            int i          = 0;
            var outputFile = Path.Combine(SettingsFile.Instance.SavePath, buildFileName(i));

            while (File.Exists(outputFile))
            {
                outputFile = Path.Combine(SettingsFile.Instance.SavePath, buildFileName(++i));
            }

            try {
                log.Debug($"Generating mp3: {outputFile}");

                var tag = new ID3TagData();
                tag.UserDefinedText.Add("CallId", Call.CallId);
                tag.UserDefinedText.Add("From", Call.From?.SkypeId);
                tag.UserDefinedText.Add("FromDisplayName", Call.From?.DisplayName);
                tag.UserDefinedText.Add("To", Call.To?.SkypeId);
                tag.UserDefinedText.Add("ToDisplayName", Call.To?.DisplayName);

                var mixer = new WaveMixerStream32 {
                    AutoStop = true
                };

                log.Debug($"  adding wave input: {_micSourceFile}");
                addMixerStream(mixer, _micSourceFile);
                log.Debug($"  adding wave input: {_spkSourceFile}");
                addMixerStream(mixer, _spkSourceFile);

                log.Debug($"  encoding");
                var wave32    = new Wave32To16Stream(mixer);
                var mp3Writer = new LameMP3FileWriter(outputFile, wave32.WaveFormat, LAMEPreset.VBR_90, tag);
                wave32.CopyTo(mp3Writer);

                // close all streams
                wave32.Close();
                mp3Writer.Close();

                log.Debug($"  finished, removing temp files");

                File.Delete(_micSourceFile);
                File.Delete(_spkSourceFile);
            }
            catch (Exception ex) {
                log.Error("Error generating mp3: " + ex.Message, ex);
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: bnylund/AudioConverter
        /// <summary>
        /// Converts a group of raw PCMs to one, final completed MP3.
        /// </summary>
        /// <param name="args">args[0] = Subdirectory of the current directory containing raw PCM files; args[1] = html recordings/ directory</param>
        static void Main(string[] args)
        {
            string dir  = Environment.CurrentDirectory;
            string html = "";

            if (args.Length > 1)
            {
#if DEBUG
                dir = args[0];
#else
                dir = Environment.CurrentDirectory + "\\" + args[0];
#endif
                html = args[1];
            }
            if (!Directory.Exists(dir))
            {
                Log("PCM directory '" + args[0] + "' invalid!", Environment.CurrentDirectory + "\\log");
                Console.WriteLine("The PCM directory doesn't exist!");
                Console.WriteLine("Correct syntax: convert.exe <PCM Directory> <HTML Directory>");
                return;
            }
            if (!Directory.Exists(html))
            {
                Log("HTML directory '" + args[0] + "' invalid!", Environment.CurrentDirectory + "\\log");
                Console.WriteLine("The HTML directory doesn't exist!");
                Console.WriteLine("Correct syntax: convert.exe <PCM Directory> <HTML Directory>");
                return;
            }
            Converting = new List <string>();

            Console.WriteLine("PCM Path: " + Path.GetFullPath(dir));
            Log("PCM Path: " + Path.GetFullPath(dir), Path.GetFullPath(dir) + "\\log");
            dir = Path.GetFullPath(dir);

            Console.WriteLine("HTML Path: " + Path.GetFullPath(html));
            Log("HTML Path: " + Path.GetFullPath(html), Path.GetFullPath(dir) + "\\log");
            html = Path.GetFullPath(html);

            pcmDir = dir;
            ProcessStartInfo info;
            List <string>    files;

            Thread dirWatcher = new Thread(new ThreadStart(() =>
            {
                Log("Thread started.");
                while (true)
                {
                    long curSize = GetDirectorySize();
                    if (curSize > HighestDirSize)
                    {
                        HighestDirSize = curSize;
                    }
                    Thread.Sleep(100);
                }
            }));
            dirWatcher.Start();

            bool quit = false;
            while (!quit)
            {
                if (DirectorySearcher.IsCompleted(dir))
                {
                    Log("Directory ready!");
                    quit = true;
                    break;
                }


                files = Directory.EnumerateFiles(dir, "*.pcm", SearchOption.TopDirectoryOnly).ToList();
                foreach (string s in files)
                {
                    if (!Converting.Contains(s))
                    {
                        FileChecker fc = new FileChecker(s);
                        if (fc.Finished)
                        {
                            Console.WriteLine(s + " finished! converting...");
                            fc.Convert();
                        }
                    }
                }
                Thread.Sleep(100);
            }


            if (File.Exists(dir + "\\done"))
            {
                File.Delete(dir + "\\done");
            }

            Converting.Clear();

            Log("Deleted done file.");

            // FIND LOWEST TICK
            string first  = Directory.EnumerateFiles(dir, "*-*.mp3", SearchOption.TopDirectoryOnly).First();
            double lowest = double.Parse(first.Split('-')[first.Split('-').Length - 1].Split('.')[0]);

            foreach (string file in Directory.EnumerateFiles(dir, "*-*.mp3", SearchOption.TopDirectoryOnly))
            {
                if (double.Parse(file.Split('-')[file.Split('-').Length - 1].Split('.')[0]) < lowest)
                {
                    lowest = double.Parse(file.Split('-')[file.Split('-').Length - 1].Split('.')[0]);
                }
            }

            Log("Lowest tick: " + lowest);

            List <WaveStream> streams = new List <WaveStream>();
            files   = Directory.EnumerateFiles(dir, "*-*.mp3", SearchOption.TopDirectoryOnly).ToList();
            streams = new List <WaveStream>();

            TryClear();
            Log("Adding files to stream...");

            for (int i = 0; i < files.Count; i++)
            {
                Console.WriteLine("Adding " + files[i] + "...");
                double           ticks   = double.Parse(files[0].Split('-')[files[0].Split('-').Length - 1].Split('.')[0]);
                Mp3FileReader    reader  = new Mp3FileReader(files[0]);
                WaveOffsetStream stream  = new WaveOffsetStream(reader, TimeSpan.FromMilliseconds(ticks - lowest), TimeSpan.Zero, reader.TotalTime);
                WaveChannel32    channel = new WaveChannel32(stream);
                channel.Volume        = 1.5F;
                channel.PadWithZeroes = false;
                streams.Add(channel);
                files.RemoveAt(0);
                i--;
            }
            WriteLine("Creating final .mp3...");
            Log("Creating final .mp3...");

            using (WaveMixerStream32 mixer = new WaveMixerStream32(streams, true))
                using (Wave32To16Stream stream = new Wave32To16Stream(mixer))
                    using (var writer = new LameMP3FileWriter(dir + "\\completed.mp3", stream.WaveFormat, 128))
                        stream.CopyTo(writer);


            WriteLine("Cleaning up...");
            Log("Cleaning up...");

            // Dispose of the streams
            foreach (WaveStream stream in streams)
            {
                try
                {
                    stream.Dispose();
                }
                catch (Exception ex) { }
            }

            streams.Clear();
            streams = null;

            dirWatcher.Abort();

            // Delete mp3 files
            DirectoryInfo idir = new DirectoryInfo(dir);
            foreach (FileInfo file in idir.GetFiles())
            {
                if (file.Name.EndsWith(".mp3") && !file.Name.Contains("completed.mp3"))
                {
                    file.Delete();
                }
            }

            try
            {
                File.Move(dir + "\\completed.mp3", html + "\\" + lowest + ".mp3");
            }
            catch (Exception ex) { Log("Unable to move completed.mp3! " + ex.Message + "\r\n" + ex.StackTrace); }

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            WriteLine("Done!");
            Log("Done!");
            Log("Highest directory size: " + ToMegabytes(HighestDirSize).ToString("N0") + "MB");
        }
コード例 #4
0
        /// <summary>
        /// Creates a mashup of two or more mp3 files by using naudio
        /// </summary>
        /// <param name="files">Name of files as an string array</param>
        /// These files should be existing in a temporay folder
        /// <returns>The path of mashed up mp3 file</returns>
        public static string CreateMashup(int projectId, string mp3Folder, string[] files)
        {
            // because there is no mash up with less than 2 files
            if (files.Count() < 2)
            {
                throw new Exception("Not enough files selected!");
            }

            try
            {
                // Create a mixer object
                // This will be used for merging files together
                var mixer = new WaveMixerStream32
                {
                    AutoStop = true
                };

                // Set the path to store the mashed up output file
                var outputFile = Path.Combine(mp3Folder, $"{projectId}.mp3");

                foreach (var file in files)
                {
                    // for each file -
                    // check if it exists in the temp folder
                    var filePath = Path.Combine(file);
                    if (File.Exists(filePath))
                    {
                        // create mp3 reader object
                        var reader = new Mp3FileReader(filePath);

                        // create a wave stream and a channel object
                        var waveStream = WaveFormatConversionStream.CreatePcmStream(reader);
                        var channel    = new WaveChannel32(waveStream)
                        {
                            //Set the volume
                            Volume = 0.5f
                        };

                        // add channel as an input stream to the mixer
                        mixer.AddInputStream(channel);
                    }
                }

                CheckAddBinPath();

                // convert wave stream from mixer to mp3
                var wave32    = new Wave32To16Stream(mixer);
                var mp3Writer = new LameMP3FileWriter(outputFile, wave32.WaveFormat, 128);
                wave32.CopyTo(mp3Writer);

                // close all streams
                wave32.Close();
                mp3Writer.Close();

                // return the mashed up file path
                return(outputFile);
            }
            catch (Exception)
            {
                // TODO: handle exception
                throw;
            }
        }