0
0

Im having a couple problems with the LAME encoder in VB. When I pass a pointer to PCM from a DSP callback to the Encoding function in LAME it works and I get a file output but when I go to play it back its all static. And then after I deinitialize lame_enc.dll my program crashes. Any help?

  • You must to post comments
0
0

Some code please ? 😀

  • You must to post comments
0
0

Here you go, I just took parts of your example and tried, instead of reading from a file, to simply take the buffer pointer from a dspcallback and encode it.

[code:2n7odbr9]
Public Sub WriteToMP3()

Dim Error As Long
Dim beConfig As PBE_CONFIG
Dim hNewFile As Long
Dim DataWrite As Long
Dim pcmPointer1 As Long, pcmPointer2 As Long
Dim mp3Pointer1 As Long, mp3Pointer2 As Long
Dim NumSamples As Long, MP3Buffer As Long, hbeStream As Long
Dim mRead As Long, mWritten As Long, mWrite As Long
Dim FileLen As Long

beConfig.dwConfig = BE_CONFIG_LAME
With beConfig.format.LHV1
    .dwStructVersion = 1
    .dwStructSize = Len(beConfig)
    .dwSampleRate = 44100
    .dwReSampleRate = 0
    .nMode = BE_MP3_MODE_JSTEREO
    .dwMpegVersion = MPEG1
    .dwPsyModel = 0
    .dwEmphasis = 0
    .bOriginal = True
    .bNoRes = True
    .nPreset = LQP_HIGH_QUALITY
    .dwBitrate = 128
End With

hNewFile = CreateFile(Output_File, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, 0)

Error = beInitStream(VarPtr(beConfig), VarPtr(NumSamples), VarPtr(MP3Buffer), VarPtr(hbeStream))
If Error <> BE_ERR_SUCCESSFUL Then
    Err.Raise Error, "Lame", GetErrorString(Error)
End If

pcmPointer1 = GlobalAlloc(&H40, PCMBuffer)
pcmPointer2 = GlobalLock(pcmPointer1)
mp3Pointer1 = GlobalAlloc(&H40, MP3Buffer)
mp3Pointer2 = GlobalLock(mp3Pointer1)

FileLen = FSOUND_Stream_GetLength(Stream)

frmMain.pbList.max = FileLen

FSOUND_Stream_Play FSOUND_FREE, Stream
StartTime = timeGetTime
frmMain.lstTracks.ListItems(Track_Num + 1).SubItems(3) = "Ripping"

Do While (mWritten < FileLen) And (Stream_Ended = False)
    If mWritten + NumSamples * 2 < FileLen Then
      mRead = NumSamples * 2
    Else
      mRead = FileLen - mWritten
    End If

    mWritten = mWritten + mRead
    mRead = mRead / 2

    Error = beEncodeChunk(hbeStream, mRead, pcmPointer1, mp3Pointer2, VarPtr(mWrite))
    If Error <> BE_ERR_SUCCESSFUL Then
        Call beCloseStream(hbeStream)
        Err.Raise Error, "Lame", GetErrorString(Error)
        Exit Sub
    End If

    If mWrite > 0 Then
        Call WriteFile(hNewFile, ByVal mp3Pointer2, mWrite, BytesWritten, 0)
    End If

    frmMain.pbList.value = mWritten
    FSOUND_Update
    DoEvents
Loop

Error = beDeinitStream(hbeStream, mp3Pointer2, VarPtr(mWrite))

If mWrite > 0 Then
    Call WriteFile(hNewFile, ByVal mp3Pointer2, mWrite, BytesWritten, 0)
End If

EndTime = timeGetTime

Call GlobalFree(mp3Pointer2)
Call GlobalFree(pcmPointer2)
Call CloseHandle(hNewFile)
Call beCloseStream(hbeStream)

Stream_Ended = False

'Close the stream
FSOUND_Stream_Stop (Stream)
FSOUND_Stream_Close (Stream)
Stream = 0
frmMain.pbList.value = 0

'Calculate elapsed time and display
If mWritten < FileLen Then
    frmMain.lstTracks.ListItems(Track_Num + 1).SubItems(3) = "Aborted"
    MsgBox "Track " & Track_Num + 1 & " was partially ripped in " & (EndTime - StartTime) / 1000 & " seconds.", vbInformation + vbOKOnly, App.Title
Else
    frmMain.lstTracks.ListItems(Track_Num + 1).SubItems(3) = "Done"
    MsgBox "Track " & Track_Num + 1 & " was successfully ripped in " & (EndTime - StartTime) / 1000 & " seconds.", vbInformation + vbOKOnly, App.Title
End If

End Sub
[/code:2n7odbr9]

  • You must to post comments
0
0

I don’t understand how you are encoding to mp3 … are you calling WriteToMP3() from your DSPCallback ? If so, you don’t need all this code, you must :

  • init your variables (malloc memory pointer, Lame encoder, fill the config type, create the file handle for output …),
  • Init fmod, create a callback, play,
  • from the dsp, fill the pcmbuffer from the DSPCallback param, call beEncodeChunk, write to the file,
  • when encoding finished, flush (write remaining bytes) and close lame encode ( beDeinitStream() ),
  • close stream handle, free memory pointer and close fmod.

It is like the wav cdda ripping except the call beEncodeChunk in the DSPCallback.

  • You must to post comments
0
0

Oh I see, what I was doing was calling this function right after I played the stream and then it would go into a loop catching the dsp pointer, encoding it, and then calling FSOUND_Update to get the next chunk of data. I’ll try it the way you said and see if it works. Thanks!

  • You must to post comments
Showing 4 results
Your Answer

Please first to submit.