in

How to Read Contents of an Audio File to a string in C#


I’m working on an app that needs to write the contents of a music file to StreamWriter which is used to create a copy of the file. My first attempt resulted in a file that couldn’t be played. Comparing the two file’s contents in Notepad++ showed a lot of differences between the two files, namely the copied file having a lot of box characters.

I thought this might be a problem with encoding, so I wrote a quick test that loops through all the System.Text.Encoding default values, for both reading and writing, to see what that would do. For my test mp3 file, it resulted in three versions that are playable, but distorted, while the others wouldn’t play at all. I also tested this with a wav file. There were three playable files again, but different encoding combinations.

Here’s my code for generating the different encoded files:

        private string FolderPath => @"D:Encoding Test";

        private string FileName => "Test Song.mp3";

        private string FilePath => Path.Combine(FolderPath, FileName);

        public void Encode()
        {
            var encodings = new[] { Encoding.ASCII, Encoding.Unicode, Encoding.UTF8, Encoding.UTF7, Encoding.UTF32, Encoding.BigEndianUnicode };

            foreach (var readerEncoding in encodings)
            {
                foreach (var writerEncoding in encodings)
                {
                    ChangeEncoding(readerEncoding, writerEncoding);
                }
            }
        }

        private void ChangeEncoding(Encoding readerEncoding, Encoding writerEncoding)
        {
            var contents = ReadFile(readerEncoding);
            WriteToNewFile(readerEncoding, writerEncoding, contents);
        }

        private string ReadFile(Encoding encoding)
        {
            using var fileStream = new FileStream(FilePath, FileMode.Open);
            using var reader = new StreamReader(fileStream, encoding);

            var contents = reader.ReadToEnd();

            return contents;
        }

        private void WriteToNewFile(Encoding readerEncoding, Encoding writerEncoding, string contents)
        {
            var newName = GetNewFileName(readerEncoding, writerEncoding);
            var newFilePath = Path.Combine(FolderPath, newName);

            using var fileStream = new FileStream(newFilePath, FileMode.Create);
            using var writer = new StreamWriter(fileStream, writerEncoding);

            writer.Write(contents);
        }

        private string GetNewFileName(Encoding readerEncoding, Encoding writerEncoding)
        {
            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(FileName);
            var extension = Path.GetExtension(FileName);

            var newName = $"{fileNameWithoutExtension}-{readerEncoding.EncodingName} to {writerEncoding.EncodingName}{extension}";
            return newName;
        }

How can I read the contents of an mp3 or wav file into a StreamWriter and get it to save to a file correctly?



Source: https://stackoverflow.com/questions/70553615/how-to-read-contents-of-an-audio-file-to-a-string-in-c-sharp

This Scrapy project uses Redis and Kafka to create a distributed on demand scraping cluster

Java RMI client waiting forever