Creating a mixer in order to resample?

Started by kafffee,

kafffee

Hello there everybody :-)

I have a (eventually mixer) channel (InputStream) that I want to encode/export to a bunch of different formats (i.e. not in real-time; wav, mp3, flac, ogg Vorbis, opus).

The user is able to choose one of these sample rates as target quality:
8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 88200, 96000, 176400, 192000

and bit depths:
8, 16, 32

and number of channels:
1, 2, 4, 6, 8

My code looks like this:

If vm.Resample Then

    Dim flags As BASSFlag = BASSFlag.BASS_STREAM_AUTOFREE Or BASSFlag.BASS_STREAM_DECODE Or BASSFlag.BASS_MIXER_END
    Select Case vm.Resampling.SelectedBitDepth
        Case 8
            flags = flags Or BASSFlag.BASS_SAMPLE_8BITS
        Case 32
            flags = flags Or BASSFlag.BASS_SAMPLE_FLOAT
    End Select
    ResamplingMixer = BassMix.BASS_Mixer_StreamCreate(vm.Resampling.SelectedSamplingRate, vm.Resampling.SelectedNumberOfChannels, flags)
    MessageBox.Show("streamcreate: " & Bass.BASS_ErrorGetCode.ToString) ' displays "BASS_ERROR_NOTAVAIL"
    BassMix.BASS_Mixer_StreamAddChannel(ResamplingMixer, InputStream, BASSFlag.BASS_MIXER_CHAN_LIMIT Or BASSFlag.BASS_MIXER_CHAN_NORAMPIN Or BASSFlag.BASS_STREAM_AUTOFREE Or BASSFlag.BASS_MIXER_CHAN_DOWNMIX)
    StreamToExport = ResamplingMixer
Else
    StreamToExport = InputStream
End If

But I get BASS_ERROR_NOTAVAIL on my BassMix.BASS_Mixer_StreamCreate call.

How come this happens and how can I fix this?

Currently InputStream is created like this:
Dim mixer = BassMix.BASS_Mixer_StreamCreate(44100, 2, BASSFlag.BASS_STREAM_DECODE Or BASSFlag.BASS_MIXER_END)
 BassMix.BASS_Mixer_StreamAddChannel(mixer, FileStreamHandle, BASSFlag.BASS_STREAM_AUTOFREE)
InputStream = mixer

Ian @ un4seen

Quote from: kafffeeBut I get BASS_ERROR_NOTAVAIL on my BassMix.BASS_Mixer_StreamCreate call.

How come this happens and how can I fix this?

That error is because the BASS_STREAM_DECODE and BASS_STREAM_AUTOFREE flags are combined in the call, which isn't possible. Removing the BASS_STREAM_AUTOFREE flag should fix the problem. And then call BASS_StreamFree when finished with the mixer.

kafffee

Alright thanks, yes that fixed it :-)

Are you okay with the sampling rates I provide to the user or should I add/remove any?

Ian @ un4seen

You seem to have all the common (and not so common) rates there :)

The OPUS encoder resamples to 48000, so there's no need for any other rate options with that. There's also no real need for a "bit depth" option with lossy formats like MP3/OGG/OPUS, as it won't affect file size; you can simply always use 32-bit float with them.

kafffee

QuoteThere's also no real need for a "bit depth" option with lossy formats like MP3/OGG/OPUS, as it won't affect file size; you can simply always use 32-bit float with them.

And what's with flac? My Windows 10 standard-app "Medienwiedergabe" (I think it's called "Movies & TV" in the English version) seems not to playback the result when I resample the mixer channel to 32 Bits before passing it to the encoder. Currently I am doing this:

EncoderHandle = BassEnc_Flac.BASS_Encode_FLAC_Start(StreamToExport, vm.FLACSets.CommandLine, BASSEncode.BASS_ENCODE_AUTOFREE Or BASSEncode.BASS_ENCODE_FP_AUTO, MP3FLACEncoderCallback, IntPtr.Zero)
But it does not return 0.

I know there are these parameters:

BASS_ENCODE_FP_8BIT, BASS_ENCODE_FP_16BIT, BASS_ENCODE_FP_24BIT

But in the .NET docs of BassEnc_Flac.BASS_Encode_FLAC_Start it says:

QuoteWhen you want to encode a floating-point channel, but the encoder does not support 32-bit floating-point sample data, then you can use one of these flags to have the sample data converted to 8/16/24 bit integer data before it's passed on to the encoder.

So what does the flac encoder need? 8, 16 or 24 Bits? Or does it actually support 32 Bits?

Ian @ un4seen

Quote from: kafffee
QuoteThere's also no real need for a "bit depth" option with lossy formats like MP3/OGG/OPUS, as it won't affect file size; you can simply always use 32-bit float with them.

And what's with flac?

FLAC is a lossless format (it preserves the original data), so the bitdepth will affect file size. It doesn't support floating-point, so floating-point data will need to be converted to integer, and BASSenc_FLAC will convert to the source's original bitdepth by default if known (or 16-bit otherwise).

If the written FLAC files are unplayable then it could be because of an issue in your MP3FLACEncoderCallback callback function (note that it needs to be able to seek). If you're just writing a file, it'll be simpler to use BASS_Encode_FLAC_StartFile instead.

kafffee

#6
QuoteIt doesn't support floating-point, so floating-point data will need to be converted to integer

So I need to do this manually with a self-written function? Or is this done by BASS_ENCODE_FP_8BIT, BASS_ENCODE_FP_16BIT, BASS_ENCODE_FP_24BIT or (can I do this automatically) by BASSEncode.BASS_ENCODE_FP_AUTO?

Quoteand BASSenc_FLAC will convert to the source's original bitdepth by default if known (or 16-bit otherwise).

What do you mean by "if known"? Do I have to tell BASSEnc_FLAC? And why will it convert to the source's original bit depth? What if it's 32 Bits as that's not supported?

Should I just disable the option to resample to 32 bit output in general then when flac is selected?