Skip to content

Instantly share code, notes, and snippets.

@profgyuri
Last active December 18, 2022 01:55
Show Gist options
  • Save profgyuri/5217fa11d12d20783699f825ba0a71c2 to your computer and use it in GitHub Desktop.
Save profgyuri/5217fa11d12d20783699f825ba0a71c2 to your computer and use it in GitHub Desktop.
Edit WAV files using NAudio

Default editing of the tags

Here is an example of how you can use the NAudio library to edit the metadata of a .wav file in C#:

    using NAudio.Wave;
    using NAudio.Wave.WaveFormats;
    
    // Open the .wav file using NAudio
    var reader = new WaveFileReader("path/to/file.wav");
    
    // Create a new WaveFileWriter to write the modified .wav file
    var writer = new WaveFileWriter("modified.wav", reader);
    
    // Set the metadata for the file
    writer.Tag.Title = "New Title";
    writer.Tag.Artist = "New Artist";
    writer.Tag.Album = "New Album";
    
    // Copy the audio data from the original file to the new file
    reader.CopyTo(writer);
    
    // Close the reader and writer
    reader.Close();
    writer.Close();

This code opens the .wav file using the WaveFileReader class, which allows you to read the audio data and metadata from the file. It then creates a new WaveFileWriter object, which allows you to write a modified version of the .wav file. You can use the Tag property of the WaveFileWriter object to set the metadata for the file, such as the title, artist, and album.

Finally, the code copies the audio data from the original file to the new file using the CopyTo method, and then closes the reader and writer. The resulting modified .wav file will have the updated metadata.

Note that this example assumes that the .wav file uses the standard waveform audio file format (RIFF wave). If the .wav file uses a different format, such as the extended waveform audio file format (RIFF wavex), you may need to use a different approach to edit the metadata.

Check encoding

You can use the NAudio library to determine which waveform audio file format (WAVE) is being used by a .wav file.

Here is an example of how you can do this in C# using NAudio:

	using NAudio.Wave;
	using NAudio.Wave.WaveFormats;

	// Open the .wav file using NAudio
	var reader = new WaveFileReader("path/to/file.wav");

	// Check the wave format of the file
	var format = reader.WaveFormat;

	// Determine whether the file uses the standard waveform audio file format (RIFF wave)
	if (format.Encoding == WaveFormatEncoding.Pcm)
	{
		Console.WriteLine("The .wav file uses the standard waveform audio file format (RIFF wave).");
	}

	// Determine whether the file uses the extended waveform audio file format (RIFF wavex)
	else if (format.Encoding == WaveFormatEncoding.Extensible)
	{
		Console.WriteLine("The .wav file uses the extended waveform audio file format (RIFF wavex).");
	}

	// Handle other wave formats as needed
	else
	{
		Console.WriteLine("The .wav file uses an unsupported waveform audio file format.");
	}

	// Close the reader
	reader.Close();

This code opens the .wav file using the WaveFileReader class, and then checks the WaveFormat property of the reader to get the wave format of the file. The WaveFormat property returns an instance of the WaveFormat class, which contains information about the audio format of the file, such as the sample rate, number of channels, and encoding.

The code then checks the Encoding property of the WaveFormat object to determine which waveform audio file format is being used by the .wav file. If the Encoding property is WaveFormatEncoding.Pcm, the file uses the standard waveform audio file format (RIFF wave). If the Encoding property is WaveFormatEncoding.Extensible, the file uses the extended waveform audio file format (RIFF wavex).

You can use this approach to determine the waveform audio file format of any .wav file, regardless of whether it uses the standard or extended format.

Edit metadata of a WAV file with extended encoding

Here is an example of how you can use the NAudio library to edit the metadata of a .wav file that uses the extended waveform audio file format (RIFF wavex) in C#:

	using NAudio.Wave;
	using NAudio.Wave.WaveFormats;

	// Open the .wav file using NAudio
	var reader = new WaveFileReader("path/to/file.wav");

	// Check the wave format of the file
	var format = reader.WaveFormat;

	// Verify that the file uses the extended waveform audio file format (RIFF wavex)
	if (format.Encoding != WaveFormatEncoding.Extensible)
	{
		Console.WriteLine("The .wav file does not use the extended waveform audio file format (RIFF wavex).");
		return;
	}

	// Cast the WaveFormat to a WaveFormatExtensible to access the metadata fields
	var extensibleFormat = (WaveFormatExtensible)format;

	// Set the metadata for the file
	extensibleFormat.SetTag("Title", "New Title");
	extensibleFormat.SetTag("Artist", "New Artist");
	extensibleFormat.SetTag("Album", "New Album");

	// Create a new WaveFileWriter to write the modified .wav file
	var writer = new WaveFileWriter("modified.wav", extensibleFormat);

	// Copy the audio data from the original file to the new file
	reader.CopyTo(writer);

	// Close the reader and writer
	reader.Close();
	writer.Close();

This code follows a similar process as the example for the standard waveform audio file format (RIFF wave), but with a few additional steps to handle the extended format. It first checks that the .wav file uses the extended waveform audio file format (RIFF wavex) by checking the Encoding property of the WaveFormat object.

If the file uses the extended format, the code casts the WaveFormat object to a WaveFormatExtensible object, which provides access to the metadata fields of the file. It then uses the SetTag method of the WaveFormatExtensible object to set the metadata for the file, such as the title, artist, and album.

Finally, it creates a new WaveFileWriter object to write the modified .wav file, copies the audio data from the original file to the new file using the CopyTo method, and closes the reader and writer. The resulting modified .wav file will have the updated metadata.

The SetTag method is part of the WaveFormatExtensible class, which is a subclass of the WaveFormat class that is specific to the extended waveform audio file format (RIFF wavex). It is not available for the standard waveform audio file format (RIFF wave).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment