.netvb.netaudionaudiocscore

Using CSCore lib to get an MP3 file sample data and information


I would like to use the library NReplayGain to calculate the replaygayn of an MP3 file and then use TagLibSharp library (with the non-official opensource replaygain support modifications) to write the ID3v2 replaygain tags into the file.

Well, this should be the pseudo-code to calculate the replaygain of a sampleset using the NReplayGain lib as they website indicates: https://github.com/karamanolev/NReplayGain

Dim trackGain As New TrackGain(samplerate, samplesize)

For Each sampleSet As SampleSet In track
    trackGain.AnalyzeSamples(sampleSet.leftSamples, sampleSet.rightSamples)
Next

Dim gain As Double = trackGain.GetGain()
Dim peak As Double = trackGain.GetPeak()

(...But If i need to be honest I don't know exactly what is a SampleSet (all the frames joined?))

before trying to calculate the ReplayGain of the sampleset I need to get the necessary data that I need to pass to the code above so I need to get the samplerate, SampleSet, leftSamples and rightSamples of an MP3 file.

I need a full code example of how I can retrieve those data using NAudio lib or any other kind of lib which could do it.

The reason why I'm asking for a full code is because with less I know that I couldn't do it by myself, I've touched before the NAudio library a little for other things and is extremelly hard for me, it seems that the libray was written just for Audio Master programmers and Audio guru's, don't have any of easy.


Solution

  • Never heard about a "sampleset". But as I can see so far, a sampleset just contains samples of the left and right channel. You could use CSCore to access all samples of a track in a quite easy way:

    Option Strict On
    
    Imports CSCore
    Imports CSCore.Codecs
    
    Module Test
    
        Sub Main()
            Dim source As IWaveSource = CodecFactory.Instance.GetCodec("C:\Temp\test.mp3")
            Dim sampleSource As ISampleSource = source.ToSampleSource()
    
            Dim sampleBuffer(source.WaveFormat.SampleRate * source.WaveFormat.Channels) As Single
            Dim sampleRate As Integer = source.WaveFormat.SampleRate
            Dim channelCount As Short = source.WaveFormat.Channels
            Dim read As Integer
    
            Dim leftSamples As New List(Of Single)
            Dim rightSamples As New List(Of Single)
    
            Do
                'now iterate through the sampleBuffer
                For i = 0 To read Step channelCount
                    If channelCount = 1 Then 'mono
                        leftSamples.Add(sampleBuffer(i))
                    ElseIf channelCount = 2 Then
                        leftSamples.Add(sampleBuffer(i))
                        rightSamples.Add(sampleBuffer(i + 1))
                    Else
                        Throw New NotSupportedException("3 or more channels are not supported.")
                    End If
                Next
            Loop While read > 0
    
            'now you've got all samples in a range of -1 to 1
            'do what ever you need to do with them
        End Sub
    
    End Module