Example #1
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 + ".ogg");

            // Don't re-encode if the original input file was also Ogg Vorbis
            if (lwav.OriginalPath != null && new string[] { ".ogg", ".logg" }.Contains(Path.GetExtension(lwav.OriginalPath), StringComparer.InvariantCultureIgnoreCase))
            {
                File.Copy(lwav.OriginalPath, output_filename, true);
            }
            else
            {
                sox.WriteFile(lwav, output_filename, encodingParameters);
            }

            using (VorbisFile file = new VorbisFile(File.ReadAllBytes(output_filename))) {
                var commentHeader = file.GetPageHeaders().Select(p => p.GetCommentHeader()).Where(h => h != null).FirstOrDefault();
                var comments      = commentHeader?.ExtractComments() ?? new VorbisComments();
                if (lwav.Looping)
                {
                    if (commentHeader == null)
                    {
                        throw new Exception("No comment header found in Ogg Vorbis file - cannot edit it to make it loop.");
                    }

                    string loopStart  = null;
                    string loopLength = null;
                    comments.Comments.TryGetValue("LOOPSTART", out loopStart);
                    comments.Comments.TryGetValue("LOOPLENGTH", out loopLength);

                    if (loopStart != lwav.LoopStart.ToString() || loopLength != lwav.LoopLength.ToString())
                    {
                        comments.Comments["LOOPSTART"]  = lwav.LoopStart.ToString();
                        comments.Comments["LOOPLENGTH"] = lwav.LoopLength.ToString();
                        using (VorbisFile newFile = new VorbisFile(file, comments)) {
                            File.WriteAllBytes(output_filename, newFile.ToByteArray());
                        }
                    }
                }
                else
                {
                    if (comments.Comments.ContainsKey("LOOPSTART") || comments.Comments.ContainsKey("LOOPLENGTH"))
                    {
                        comments.Comments.Remove("LOOPSTART");
                        comments.Comments.Remove("LOOPLENGTH");
                        using (VorbisFile newFile = new VorbisFile(file, comments)) {
                            File.WriteAllBytes(output_filename, newFile.ToByteArray());
                        }
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            byte[] b = File.ReadAllBytes(@"test.ogg");

            using (VorbisFile file = new VorbisFile(b)) {
                List <OggPage> pageHeaders = file.GetPageHeaders();

                VorbisHeader commentHeader = null;
                foreach (var pageHeader in pageHeaders)
                {
                    commentHeader = pageHeader.GetCommentHeader();
                    if (commentHeader != null)
                    {
                        break;
                    }
                }

                if (commentHeader == null)
                {
                    throw new Exception("comment header not found");
                }

                if (commentHeader.PacketType == 3)
                {
                    var c = commentHeader.ExtractComments();

                    Console.WriteLine(c.Vendor);
                    Console.WriteLine(string.Join(", ", c.Comments.Select(p => p.Key + ": " + p.Value)));

                    c.Comments["LOOPSTART"]  = "100000";
                    c.Comments["LOOPLENGTH"] = "150000";

                    using (VorbisFile newFile = new VorbisFile(file, c)) {
                        File.WriteAllBytes("out.ogg", newFile.ToByteArray());
                    }
                }
            }
        }