Author Topic: Using BASSMix to recode PCM / Wave data  (Read 51 times)

Gazoo

  • Posts: 54
Using BASSMix to recode PCM / Wave data
« on: 2 May '21 - 12:04 »
Hey BASS forum,

I'm currently using BASS to load raw PCM data from .mp3 files. If the sample rate isn't 44.1 Khz or the audio is in Mono, I'd like to recode it to 44.1 Khz / Stereo. I've found a number of examples using BASSMix to do this, but they all connect with a file:

https://www.un4seen.com/forum/?topic=15531.msg108129#msg108129
https://www.un4seen.com/forum/?topic=17333.msg121548;topicseen#msg121548

Thus far I've tried to put this code together to just do it in-memory providing the raw PCM wave data:

Code: [Select]
auto streamMixer = BASS_Mixer_StreamCreate( BuildOptions::SampleRate, 2, BASS_MIXER_END | BASS_STREAM_DECODE );

auto success = BASS_StreamPutData( streamMixer, data.get(), sizeInBytes );

int64_t audioLengthInBytes = BASS_ChannelGetLength( streamMixer, BASS_POS_BYTE );

////BASS_StreamPutData( instream, buffer, length );
while (1) {
BYTE buf[20000];
if (BASS_ChannelGetData( streamMixer, buf, sizeof( buf ) ) == -1) break; // process the mixer until it fails/ends
}

But the ChannelGetData appears to return nothing. Do I need a separate stream or? What am I missing?

Thanks in advance,

Gazoo

Ian @ un4seen

  • Administrator
  • Posts: 23545
Re: Using BASSMix to recode PCM / Wave data
« Reply #1 on: 3 May '21 - 15:43 »
You had the right idea with BASS_StreamPutData but you will need to create a "push" stream for that rather than using it directly on the mixer. For example, you could modify your code like this:

Code: [Select]
auto streamMixer = BASS_Mixer_StreamCreate( BuildOptions::SampleRate, 2, BASS_MIXER_END | BASS_STREAM_DECODE );
auto streamPush = BASS_StreamCreate(freq, chans, BASS_STREAM_DECODE, STREAMPROC_PUSH, 0);
BASS_Mixer_StreamAddChannel(streamMixer, streamPush, BASS_MIXER_CHAN_NORAMPIN);

auto success = BASS_StreamPutData( streamPush, data.get(), sizeInBytes | BASS_STREAMPROC_END );

while (1) {
BYTE buf[20000];
if (BASS_ChannelGetData( streamMixer, buf, sizeof( buf ) ) == -1) break; // process the mixer until it fails/ends
}

The BASS_STREAMPROC_END flag means there's no more data. Remove that if there's more to come, but note BASS_ChannelGetData will return 0 rather than -1 until it's set.