0

I am using libsndfile (with the c# wrapper) to create wav/aiff files based on existing wav files by for example converting a stereo file to mono file or vice versa.

My process is:

  1. Read existing file
  2. Open new file for write while filling LibsndfileInfo
  3. WriteItems into the new file

                LibsndfileInfo fInfo = new LibsndfileInfo();
                fInfo.Format = info.Format;
                fInfo.Channels = info.Channels;
                fInfo.SampleRate = info.SampleRate;              
    
                IntPtr sndOutFile = api.Open(outfilename, LibsndfileMode.Write, ref fInfo);
    
                api.WriteItems(sndOutFile, data, num_items);
    
                api.Close(sndOutFile);
    

While doing that I have noticed that any extra meta data (extra chunks) that were in the origin file are lost in the resulting file.

Is there a way to somehow bringing these extra chunks along or copy the header over to the new file using libsndfile ?

thanks for any input.

Mike

Mozzak
  • 201
  • 3
  • 16

1 Answers1

0

I don't know the C# wrapper, but libsndfile C and C++ api have the command function that allow reading and writing info contained in the chunk like broadcast info and other string:

// Open input file
SndfileHandle inputFile(inputFileName, SFM_READ);

// Read broadcast info:
SF_BROADCAST_INFO binfo;
file.command(SFC_GET_BROADCAST_INFO, &binfo, sizeof (binfo));

std::cout() << binfo.description;

// Open output file with same parameters:
SndfileHanle outputFile(outputFileName, SFM_WRITE, inputFile.format(), inputFile.channels(), inputFile.samplerate());

// Copy broadcast info:
outputFile.command(SFC_SET_BROADCAST_INFO, &binfo, sizeof (binfo));

// Copy other string info:
outputFile.setString(SF_STR_TITLE, inputFile.getString(SF_STR_TITLE));
outputFile.setString(SF_STR_COPYRIGHT, inputFile.getString(SF_STR_COPYRIGHT));
outputFile.setString(SF_STR_SOFTWARE, inputFile.getString(SF_STR_SOFTWARE));
outputFile.setString(SF_STR_ARTIST, inputFile.getString(SF_STR_ARTIST));
outputFile.setString(SF_STR_COMMENT, inputFile.getString(SF_STR_COMMENT));
outputFile.setString(SF_STR_DATE, inputFile.getString(SF_STR_DATE));
outputFile.setString(SF_STR_ALBUM, inputFile.getString(SF_STR_ALBUM));
outputFile.setString(SF_STR_LICENSE, inputFile.getString(SF_STR_LICENSE));
outputFile.setString(SF_STR_TRACKNUMBER, inputFile.getString(SF_STR_TRACKNUMBER));
outputFile.setString(SF_STR_GENRE, inputFile.getString(SF_STR_GENRE));

// Copy sound data:
....
Martin Delille
  • 11,360
  • 15
  • 65
  • 132