This project has moved. For the latest updates, please go here.

Writing new chunk data

Apr 18, 2011 at 7:33 PM

Before I begin please let me apologise in advance if this topic has been covered elsewhere, although I have had a good look and not found exactly what I need to know.

I've been using NAudio to create an app for browsing and previewing audio samples when composing music, mainly to exercise my programming (I've only been coding for about a year), and NAudio has proved invaluable. I've created a class to parse the ACID chunk data available in some wav samples, which so far reads and writes ACID info to any wavs which already have the chunk present. Unfortunately I haven't been able to append new chunk data to wavs which do not have an ACID chunk. I was wondering if there are any NAudio specific methods which would help with this? I've browsed the source pretty carefully, but being fairly new to this there is always a good chance I've missed something. I have found this topic http://stackoverflow.com/questions/1550819/how-to-write-cues-markers-to-a-wav-file-in-net which helped me come up with the following:

            FileStream fs;
            BinaryWriter bw;

            if (_chunkExists)
            {
                //update current chunk from chunk position
                try
                {                   
                    fs = new FileStream(_fileName, FileMode.Open);
                    fs.Position = _chunkPosition;
                    bw = new BinaryWriter(fs);
                    bw.Write(chunkData, 0, chunkData.Length);
                    bw.Close();
                    fs.Dispose();
               
                }
                catch (Exception e)
                {
                    throw new AcidTagException("Failed to update ACID chunk", e);
                }
            }
            else
            {
                //append chunk to file
                try
                {
                    //this is writing to the file - read only files throw an exception - but the tag is not visible.
                    //is it a formatting problem (of the chunk itself) or because it's appending the chunk at the end?
                    fs = new FileStream(_fileName, FileMode.Append, FileAccess.Write);
                    bw = new BinaryWriter(fs);
                    char[] acid = new char[4] {'a', 'c', 'i', 'd' };
                    bw.Write(acid, 0, 4); //chunk Id
                    bw.Write((int)24); //chunk Size
                    bw.Write(chunkData, 0, chunkData.Length); //chunk data
                    bw.Close();
                    fs.Dispose();
                    
                }
                catch (Exception e)
                {
                    throw new AcidTagException("Failed to Write ACID chunk", e);
                }
                _chunkExists = true;
            }

it works when updating an existing chunk, but appending to a file appears not to work. I'm fairly certain it writes data to the file, as attempting to write to a read-only wav throws an exception, but reparsing the wav file states that there is no ACID data available. Any advice would be hugely appreciated. :D

TIA

Coordinator
Apr 21, 2011 at 4:51 PM

Yes, this is a feature we could do with improving in NAudio. The WaveFileWriter class shows how to write a fact chunk, but doesn't currently support adding any other chunks. I'll put it on my list for NAudio 1.5.

I'm impressed if you've managed to parse ACID. A while ago I attempted to decipher the strc chunks and never worked it out completely (look in WaveFileInspector.cs)

Apr 21, 2011 at 7:17 PM

Ah, well I'm glad I'm not being completely blind :)

I had noticed the strc chunk code, which was in fact what pointed me to the BitConverter class (still learning a lot of this). As I understand it these chunks are needed to truely 'acidize' a wave file, as well as the Acid chunk, and wavs without it can have compatability problems in some software. The AcidTag class in it's current state doesn't have a problem with this as it only updates existing Acid chunks (and therefore wavs which should have existing strc chunks - which remain untouched), but when it comes to appending a new chunk I figure a new strc chunk will need to be added too. Even if you manage to fully decipher the strc I'm not sure how easy it would be to create without either a fairly complex gui or some sort of transient detection to split the file at it's peaks. I did have some half brained idea about taking the length of the file and dividing it by the number of/double the number of beats stored in the Acid chunk, but this is making some huge assumptions, least of all that the number of beats has even been filled in by the user, let alone that this would be enough information to qualify as a proper strc chunk. Seeing as the class does enough for me now (my browser app is stable enough to actually be useful - but is far from releasable) I'm happy to share it with anyone else who may have a use for it.

It can be downloaded here.

There are probably a few improvements to be made to error checking/handling, and of course it doesn't properly append new chunks, but it should at least help detail some of what goes on in the Acid chunk, as this information seems pretty darn hard to track down.

Thanks for the input

Matt

Coordinator
Apr 25, 2011 at 1:07 PM

thanks for sharing, looks useful.

Feb 15, 2015 at 12:22 AM
Hi guys, sorry to reply on old thread but this was only information I could find on acid wavs.

I wanted to ask, did you in the meantime found a way to decrypt strc or bmrk chunks?
Coordinator
Feb 16, 2015 at 3:54 PM
I'm afraid I don't have any further information on this. Sadly it is not an open format.