public void WriteFile(PCM16Audio lwav, string output_dir, string original_filename_no_ext)
        {
            string outPath = Path.Combine(output_dir, original_filename_no_ext + ".mp3");

            if (outPath.Contains("\""))
            {
                throw new AudioExporterException("Invalid character (\") found in output filename");
            }

            string infile = TempFiles.Create("wav");

            File.WriteAllBytes(infile, lwav.Export());

            ProcessStartInfo psi = new ProcessStartInfo {
                FileName        = ExePath,
                UseShellExecute = false,
                CreateNoWindow  = true,
                Arguments       = "--silent " + EncodingParameters + " " + infile + " \"" + outPath + "\""
            };
            Process p = Process.Start(psi);

            p.WaitForExit();
            File.Delete(infile);

            if (p.ExitCode != 0)
            {
                throw new AudioExporterException("LAME quit with exit code " + p.ExitCode);
            }
        }
        /// <summary>
        /// Writes the LWAV to the given file, using SoX to make sure the format is correct. SoX can encode and write FLAC and Ogg Vorbis files, among others.
        /// </summary>
        /// <param name="lwav">Input audio</param>
        /// <param name="output_filename">Path of output file</param>
        public void WriteFile(PCM16Audio lwav, string output_filename, string encodingParameters = null)
        {
            if (output_filename.Contains('"'))
            {
                throw new AudioImporterException("File paths with double quote marks (\") are not supported");
            }

            string infile = TempFiles.Create("wav");

            File.WriteAllBytes(infile, lwav.Export());

            ProcessStartInfo psi = new ProcessStartInfo {
                FileName        = ExePath,
                Arguments       = infile + " " + (encodingParameters ?? "") + " \"" + output_filename + "\"",
                UseShellExecute = false,
                CreateNoWindow  = true
            };
            Process p = Process.Start(psi);

            p.WaitForExit();

            File.Delete(infile);

            if (p.ExitCode != 0)
            {
                throw new AudioExporterException("SoX quit with exit code " + p.ExitCode);
            }
        }
Esempio n. 3
0
        public Task WriteFileAsync(PCM16Audio lwav, string output_dir, string original_filename_no_ext)
        {
            string output_filename = Path.Combine(output_dir, original_filename_no_ext + ".wav");

            File.WriteAllBytes(output_filename, lwav.Export());
            return(Task.FromResult(0));
        }
Esempio n. 4
0
        public async Task WriteFileAsync(PCM16Audio lwav, string output_dir, string original_filename_no_ext)
        {
            string outPath = Path.Combine(output_dir, original_filename_no_ext + ".mp3");

            if (outPath.Contains("\""))
            {
                throw new AudioExporterException("Invalid character (\") found in output filename");
            }

            if (lwav.OriginalMP3 != null)
            {
                File.WriteAllBytes(outPath, lwav.OriginalMP3);
                return;
            }

            string infile = TempFiles.Create("wav");

            File.WriteAllBytes(infile, lwav.Export());

            ProcessStartInfo psi = new ProcessStartInfo {
                FileName        = ExePath,
                UseShellExecute = false,
                CreateNoWindow  = true,
                Arguments       = "--silent " + EncodingParameters + " " + infile + " \"" + outPath + "\""
            };
            var pr = await ProcessEx.RunAsync(psi);

            File.Delete(infile);

            if (pr.ExitCode != 0)
            {
                throw new AudioExporterException("LAME quit with exit code " + pr.ExitCode);
            }
        }
Esempio n. 5
0
        public async Task WriteFileAsync(PCM16Audio lwav, string output_dir, string original_filename_no_ext)
        {
            string outPath = Path.Combine(output_dir, original_filename_no_ext + (Adts ? ".aac" : ".m4a"));

            if (outPath.Contains("\""))
            {
                throw new AudioExporterException("Invalid character (\") found in output filename");
            }

            string infile = TempFiles.Create("wav");

            File.WriteAllBytes(infile, lwav.Export());

            ProcessStartInfo psi = new ProcessStartInfo {
                FileName        = ExePath,
                UseShellExecute = false,
                CreateNoWindow  = true,
                Arguments       = $"--silent {(Adts ? "--adts " : "")} {EncodingParameters} {infile} -o \"{outPath}\""
            };
            var pr = await ProcessEx.RunAsync(psi);

            File.Delete(infile);

            if (pr.ExitCode != 0)
            {
                foreach (string s in pr.StandardError)
                {
                    Console.WriteLine(s);
                }
                throw new AudioExporterException("qaac quit with exit code " + pr.ExitCode);
            }
        }
Esempio n. 6
0
        public void WriteFile(PCM16Audio lwav, string output_dir, string original_filename_no_ext)
        {
            AudioData audio = lwav.OriginalAudioData ?? new WaveReader().Read(lwav.Export());

            audio.SetLoop(lwav.Looping, lwav.LoopStart, lwav.LoopEnd);
            byte[] data = GetData(audio);
            File.WriteAllBytes(Path.Combine(output_dir, original_filename_no_ext + GetExtension()), data);
        }
Esempio n. 7
0
        public void WriteFile(PCM16Audio lwav, string output_dir, string original_filename_no_ext)
        {
            string output_filename = Path.Combine(output_dir, original_filename_no_ext + ".wav");
            int    length          = lwav.Samples.Length / lwav.Channels;
            string filename        = Path.GetFileNameWithoutExtension(lwav.OriginalPath);

            if (lwav.Looping)
            {
                if (!txt.Contains(filename))
                {
                    txt += $"{lwav.LoopStart} {lwav.LoopEnd} {lwav.Samples.Length / lwav.Channels} {Path.GetFileNameWithoutExtension(lwav.OriginalPath)}.wav\r\n";
                }
            }
            File.WriteAllBytes(output_filename, lwav.Export());
        }
        /// <summary>
        /// Applies one or more SoX effects to the LWAV given and reads the result into a new LWAV.
        /// Intended to either adjust the volume of the audio or reduce the file size.
        /// </summary>
        /// <param name="lwav">The LWAV to use as an input</param>
        /// <param name="max_channels">The new number of channels (if the LWAV already has this number of channels or fewer, this effect will not be applied)</param>
        /// <param name="db">Volume adjustment, in decibels (if 0, this effect will not be applied)</param>
        /// <param name="amplitude">Volume adjustment, in linear ratio (if 1, this effect will not be applied)</param>
        /// <param name="max_rate">The new sample rate (if the LWAV's sample rate is less than or equal to this value, this effect will not be applied)</param>
        /// <returns>A new LWAV object if one or more effects are applied; the same LWAV object if no effects are applied.</returns>
        public PCM16Audio ApplyEffects(PCM16Audio lwav, int max_channels = int.MaxValue, decimal db = 0, decimal amplitude = 1, int max_rate = int.MaxValue)
        {
            byte[] wav = lwav.Export();

            int channels   = Math.Min(max_channels, lwav.Channels);
            int sampleRate = Math.Min(max_rate, lwav.SampleRate);

            StringBuilder effects_string = new StringBuilder();

            if (channels != lwav.Channels)
            {
                effects_string.Append(" channels " + max_channels);
            }
            if (db != 0)
            {
                effects_string.Append(" vol " + db + " dB");
            }
            if (amplitude != 1)
            {
                effects_string.Append(" vol " + amplitude + " amplitude");
            }
            if (sampleRate != lwav.SampleRate)
            {
                effects_string.Append(" rate " + max_rate);
            }

            if (effects_string.Length == 0)
            {
                // No effects will be performed - just return the same LWAV that was passed in without calling SoX unnecessarily
                return(lwav);
            }

            string infile  = TempFiles.Create("wav");
            string outfile = TempFiles.Create("wav");

            File.WriteAllBytes(infile, wav);

            // Sometimes when SoX changes sample rate and sends the result to stdout, it gives the wrong length in the data chunk. Let's just have it send us raw PCM data instead.
            ProcessStartInfo psi = new ProcessStartInfo {
                FileName        = ExePath,
                UseShellExecute = false,
                CreateNoWindow  = true,
                Arguments       = "-t wav " + infile + " -t wav " + outfile + " " + effects_string
            };
            Process p = Process.Start(psi);

            p.WaitForExit();
            File.Delete(infile);

            try {
                PCM16Audio l = PCM16Factory.FromFile(outfile, true);
                l.Looping   = lwav.Looping;
                l.LoopStart = lwav.LoopStart;
                l.LoopEnd   = lwav.LoopEnd;

                if (l.Looping && l.SampleRate != lwav.SampleRate)
                {
                    // When the sample rate is changed, we need to change the loop points to match.
                    double ratio = (double)l.SampleRate / lwav.SampleRate;
                    l.LoopStart = (int)(l.LoopStart * ratio);
                    l.LoopEnd   = (int)(l.LoopEnd * ratio);
                }
                return(l);
            } catch (Exception e) {
                throw new AudioImporterException("Could not read SoX output: " + e.Message);
            }
        }