hi,
i am using c# dotnet core..
i have a server app that will get byte[] mp3 data ,
i want to encode to 16000hz mono pcm without writing to file, i just want to get encoded pcm byte[]
i tried this but not get success.
need help
public async Task<byte[]> GetFromMp3File(byte[] bytes)
{
try
{
// Initialize BASS
if (!Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero))
throw new Exception("BASS initialization failed");
int stream = Bass.BASS_StreamCreatePush(16000, 1, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_STREAM_AUTOFREE, IntPtr.Zero);
if (stream == 0)
throw new Exception("Error creating BASS stream.");
// Decode the MP3 stream to WAV
using var inputStream = new MemoryStream(bytes);
inputStream.Seek(0, SeekOrigin.Begin);
using var wavStream = new MemoryStream();
int encoder= BassEnc.BASS_Encode_Start(stream, null, BASSEncode.BASS_ENCODE_PCM , null, IntPtr.Zero);
var err = Bass.BASS_ErrorGetCode();
byte[] bufferOut = new byte[65536]; // adjust the size as needed
byte[] bufferIn = new byte[65536]; // adjust the size as needed
int readIn = 0;
int totalReadIn = bytes.Length;
while (true)
{
if (readIn < totalReadIn)
{
readIn = await inputStream.ReadAsync(bufferIn, readIn, bufferIn.Length);
var putIn=Bass.BASS_StreamPutData(stream, bufferIn, readIn);
}
int bytesRead = Bass.BASS_ChannelGetData(encoder, bufferOut, bufferOut.Length);
if (bytesRead == 0) break;
if(bytesRead>0) await wavStream.WriteAsync(bufferOut, 0, bytesRead);
}
BassEnc.BASS_Encode_Stop(stream);
Bass.BASS_StreamFree(stream);
Bass.BASS_Free();
wavStream.Seek(0, SeekOrigin.Begin);
var b= wavStream.ToArray();
return b;
}
catch (Exception ex)
{
// _logger.LogError(ex, "Error in GetFromMp3");
return new byte[0];
}
}