Author Topic: Generate pure tone  (Read 962 times)

ouestaudition

  • Posts: 21
Generate pure tone
« on: 1 Apr '13 - 01:44 »
Hi,

I would like to be able to generate a pure tone signal at a certain frequency and at a certain level using Bass.
This signal should be continuous or pulsing.

I can't find the way to do that.

Could you give me a direction to follow to do that.

Thank's a lot


Art60

  • Posts: 137
Re: Generate pure tone
« Reply #1 on: 1 Apr '13 - 11:02 »
Hi,

I quite certain there are other ways to achieve what you need to do.
But my first thought would be to create a sine wave (y=sin(x) if you're float or y=sin(x)*32767 if you're 16bits integer) in memory then loop while playing it...

Hope this starts the reflexion :)

[EDIT]
The whole project you need may be synth.c Ian already supplies with BASS
« Last Edit: 1 Apr '13 - 11:58 by Art60 »

Lorni

  • Posts: 49
Re: Generate pure tone
« Reply #2 on: 1 Apr '13 - 15:46 »
Look at the BASS_SampleCreate function in the manual.

It contains good example:
Create a 440hz sine wave sample.

Code: [Select]
HSAMPLE sample=BASS_SampleCreate(256, 28160, 1, 1, BASS_SAMPLE_LOOP|BASS_SAMPLE_OVER_POS); // create sample
short data[128]; // data buffer
int a;
for (a=0; a<128; a++)
    data[a]=(short)(32767.0*sin((double)a*6.283185/64)); // sine wave
BASS_SampleSetData(sample, data); // set the sample's data

David_AVD

  • Posts: 53
Re: Generate pure tone
« Reply #3 on: 14 Oct '21 - 04:32 »
Does anyone have a Delphi version of this please?

Chris

  • Posts: 1989
Re: Generate pure tone
« Reply #4 on: 14 Oct '21 - 09:56 »
Hi, untested so something like that

Code: [Select]
var
   Sample: HSAMPLE;
   Data: array [0 .. 128] of ShortInt;
   ch: HCHANNEL;

function CreateSample(): boolean;
begin
  sample := BASS_SampleCreate(256, 28160, 1, 1, BASS_SAMPLE_LOOP or BASS_SAMPLE_OVER_POS); // create sample
  // Inplace variable for Loop
 for var a := 0 to Length(Data) - 1 do
  begin
    Data[a] := ShortInt(round(32767 * double(sin(a * 6.283185 / 64)))); // sine wave
    BASS_SampleSetData(sample, @Data); // set the sample's data
  end;
  // Play Stuff
      ch := BASS_SampleGetChannel(sample, 0);
    Result := (ch <> 0) and BASS_ChannelPlay(ch, False) and  (Bass_ErrorGetCode = 0) ;
end;
« Last Edit: 14 Oct '21 - 14:55 by Chris »

David_AVD

  • Posts: 53
Re: Generate pure tone
« Reply #5 on: 15 Oct '21 - 02:01 »
I have that code working with a change of ShortInt to Small Int.

While I can start the tone and end the tone with BASS_ChannelStop and BASS_SampleFree, the next time I try and play it I get silence.

Chris

  • Posts: 1989
Re: Generate pure tone
« Reply #6 on: 15 Oct '21 - 08:57 »
Hi, I have tested it (Delphi 11) with that working Code

Code: [Select]
var
  Form1: TForm1;
  sample: HSAMPLE;
  Data: array [0 .. 128] of SmallInt;
  ch: HChannel;

Code: [Select]
procedure CreateSample();
begin
  if sample <> 0 then
    Bass_SampleFree(sample);
  sample := BASS_SampleCreate(256, 28160, 1, 1, BASS_SAMPLE_LOOP or BASS_SAMPLE_OVER_POS); // create sample
  for var a := 0 to Length(Data) - 1 do
  begin
    Data[a] := SmallInt(round(32767 * double(sin(a * 6.283185 / 64)))); // sine wave
    BASS_SampleSetData(sample, @Data); // set the sample's data
  end;
end;

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  CreateSample();
  ch := BASS_SampleGetChannel(sample, 0);
  BASS_ChannelPlay(ch, False);
end;

Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
begin
  BASS_ChannelStop(ch);
  Bass_SampleFree(sample);
end;

will here working fine

David_AVD

  • Posts: 53
Re: Generate pure tone
« Reply #7 on: 18 Oct '21 - 07:20 »
The only difference I found is that I'm using BASS_SetDevice before playing so I can direct the tone to the selected output device.

If I remove the BASS_SetDevice call it works as expected and I can start and stop correctly.


David_AVD

  • Posts: 53
Re: Generate pure tone
« Reply #8 on: 18 Oct '21 - 07:44 »
OK, it looks like BASS_ChannelSetDevice is what I should be using?

Chris

  • Posts: 1989
Re: Generate pure tone
« Reply #9 on: 18 Oct '21 - 08:48 »
are you working with multiple Devices ?
BASS_SetDevice will select a other device while Bass_ChannelSetDevice will switch a Stream to a other Devive.
Remarks from the doc
When changing a sample's device, all the sample's existing channels (HCHANNELs) are freed. It is not possible to change the device of an individual sample channel.


end;

David_AVD

  • Posts: 53
Re: Generate pure tone
« Reply #10 on: 18 Oct '21 - 11:14 »
Thanks for your help. I got it all going by using Bass_ChannelSetDevice.

If the user selects a different device while the tone is playing, I simply stop it and recreate / start it on the new device.