Author Topic: Save the created sample to a file  (Read 112 times)

tumanovalex

  • Posts: 17
Save the created sample to a file
« on: 17 May '21 - 16:13 »
Tell me, please, how after creating the sample:
Code: [Select]
float amp = 32767f;
float sampleRate = 64f;
float freq = 440f;
int sampleLenght = 256;
Bass.BASS_Init(-1, (int)sampleRate, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero);
int sample = Bass.BASS_SampleCreate(sampleLenght, (int)(freq * sampleRate), 1, 1, BASSFlag.BASS_SAMPLE_LOOP | BASSFlag.BASS_SAMPLE_OVER_POS);
short[] data = new short[sampleLenght / 2]; // data buffer
for (int i = 0; i < sampleLenght / 2; i++)
data[i] = (short)(amp * Math.Sin(i * 2f * Math.PI / sampleRate)); // sine wave - bass
if (!Bass.BASS_SampleSetData(sample, data )) ShowMes("Error SampleSetData");
int channel = Bass.BASS_SampleGetChannel(sample, true);
if(!Bass.BASS_ChannelPlay(channel, true)) ShowMes("Error ChannelPlay");
save it to file MP3

tumanovalex

  • Posts: 17
Re: Save the created sample to a file
« Reply #1 on: 18 May '21 - 15:02 »
In program
Code: [Select]
float amp = 32767f;
float sampleRate = 64f;
float freq = 440f;
int sampleLenght = 256;
if(!Bass.BASS_Init(-1, (int)sampleRate, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero))
  ShowMes("Error Init");
int sample = Bass.BASS_SampleCreate(sampleLenght, (int)(freq * sampleRate), 1, 1, BASSFlag.BASS_SAMPLE_LOOP |
             BASSFlag.BASS_SAMPLE_OVER_POS);
if (sample == 0)
  ShowMes("Error SampleCreate");
short[] data = new short[sampleLenght / 2]; // data buffer
for (int i = 0; i < sampleLenght / 2; i++)
  data[i] = (short)(amp * Math.Sin(i * 2f * Math.PI / sampleRate)); // sine wave - bass
if (!Bass.BASS_SampleSetData(sample, data ))
  ShowMes("Error SampleSetData");
int channel = Bass.BASS_SampleGetChannel(sample, true);
if(!Bass.BASS_ChannelPlay(channel, true))
  ShowMes("Error ChannelPlay");
if(BassEnc_Mp3.BASS_Encode_MP3_StartFile(channel, null, Un4seen.Bass.AddOn.Enc.BASSEncode.BASS_ENCODE_DEFAULT, "2.mp3") == 0)
  ShowMes("MP3_StartFile");
get error BASS_ERROR_HANDLE" for method BASS_Encode_MP3_StartFile. Why does this bug appear?

Ian @ un4seen

  • Administrator
  • Posts: 23743
Re: Save the created sample to a file
« Reply #2 on: 18 May '21 - 15:33 »
It isn't possible to set an encoder on a sample (HCHANNEL). You will need to use a stream instead. You could do that by calling BASS_StreamCreate with proc=STREAMPROC_PUSH (BASS_StreamCreatePush if using BASS.Net) instead of BASS_SampleCreate, and then BASS_StreamPutData instead of BASS_SampleSetData. Please see the documentation for details on those 2 functions.

tumanovalex

  • Posts: 17
Re: Save the created sample to a file
« Reply #3 on: 18 May '21 - 17:10 »
I fixed the code:
Code: [Select]
int stream = Bass.BASS_StreamCreatePush((int)freq, 1, BASSFlag.BASS_DEFAULT, IntPtr.Zero);
if (stream == 0)
  ShowMes("Error StreamCreate");
int streamData = Bass.BASS_StreamPutData(stream, data, 128);
if(streamData == -1)
  ShowMes("Error StreamPutData");
if(BassEnc_Mp3.BASS_Encode_MP3_StartFile(streamData, null, Un4seen.Bass.AddOn.Enc.BASSEncode.BASS_ENCODE_DEFAULT, "2.mp3") == 0)
  ShowMes("Error MP3_StartFile");
However, the same error appears. I apologize for being insensitive, but I work with sound for the first time and for me a lot is incomprehensible.

Ian @ un4seen

  • Administrator
  • Posts: 23743
Re: Save the created sample to a file
« Reply #4 on: 18 May '21 - 17:34 »
You should use the "stream" handle (instead of "streamData") in the BASS_Encode_MP3_StartFile call. You will also need to play the stream (BASS_ChannelPlay) to have the data processed/encoded. If you don't want to play it then another option is to use BASS_Encode_Write instead of BASS_StreamPutData:

Code: [Select]
int stream = Bass.BASS_StreamCreatePush((int)freq, 1, BASSFlag.BASS_DEFAULT, IntPtr.Zero);
if (stream == 0)
  ShowMes("Error StreamCreate");
if(BassEnc_Mp3.BASS_Encode_MP3_StartFile(stream, null, Un4seen.Bass.AddOn.Enc.BASSEncode.BASS_ENCODE_DEFAULT, "2.mp3") == 0)
  ShowMes("Error MP3_StartFile");
BassEnc.BASS_Encode_Write(stream, data, 128); // encode data
BassEnc.BASS_Encode_Stop(stream); // close encoder

tumanovalex

  • Posts: 17
Re: Save the created sample to a file
« Reply #5 on: 18 May '21 - 18:49 »
Thank you very much for the answer, code and patience. It all worked out!