SFML community forums
Help => Audio => Topic started by: Unseen Machine on April 08, 2011, 08:33:36 pm
-
First Hi,
I am making a wrapper so QB64 http://www.qb64.net can have use of at least some parts of SFML (namely OpenGL) but i have come to a stumbling block.
In QB64 there is a command called _SNDRAW which takes data (ranging from -1 to 1) at the sample rate of the sound card. The data sent is the speakers position at that point in time -1 = fully down, 0 normal, 1 = fully up.
I would like to be able to put the data from a SoundBuffer into an array i pass from QB64 to my C++ function, so i can have access to each sample's value and then convert it to be compatible with _SNDRAW.
How do i access the values of each sample in a SoundBuffer?
In what format is the data from SoundBuffer passed to the sound card?
Any help would be great,
Many thanks,
John
-
How do i access the values of each sample in a SoundBuffer?
GetSamples().
In what format is the data from SoundBuffer passed to the sound card?
16 bits signed integers.
By the way, everything's explained in the documentation ;)
-
By the way, everything's explained in the documentation ;)
That should be in your signature :lol:
-
I read the docs, they are excellant by the way.
The problem is QB64 cant use pointers. I need to fill an array i dimension in QB64 and pass to my C++ function, as i cant use pointers i need to get each individual sample from SOundBuffer and put it into my array, which i can then use in QB64.
Any ideas?
John
-
Maybe if i explain it differantly.
I want to be able to get the value of a single sample from SoundBuffer.
Some thing like (not that this will work, just an idea of what i am after)
int SF_Sound_Buffer_Get_Sample(int SampleNum)
{
return(SndBuffer.GetSample(SampleNum));
}
Is it possible?
Thanks,
John
-
The pointer returned by GetSamples() points at the array of samples, so...
buffer.GetSamples()[SampleNum]
-
I will look into it and report back later. SFML kicks SDL's butt by the way..
-
Works like a dream, many thanks.
John
-
I got a few more questions on the same topic,
How many samples can a SoundBuffer contain?
Is it set or system limited (by memory)?
Is there a Filesize limit?
I have not looked into Music yet, can you get the samples from that too?
Thanks and sorry for being a pest.
John
-
How many samples can a SoundBuffer contain?
Is it set or system limited (by memory)?
I think there's no limit, except the available memory.
Is there a Filesize limit?
No.
I have not looked into Music yet, can you get the samples from that too?
It's not its purpose. If you want to access the samples of an audio file, use sf::SoundBuffer.
Thanks and sorry for being a pest
I think you should write code instead of asking every possible question that comes to your mind ;)
-
Well here's what have done so far (built for QB64). The thred on qb64 forum is here...http://www.qb64.net/forum/index.php?topic=3204.0
I appreciate the replies thuough Laurent, many thanks.
//SFML Wrapper for QB64 v.02 - By John Onyon a.k.a Unseen Machine
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>
sf::RenderWindow App;
sf::Color BackgroundColor = sf::Color(0,0,0,255);
sf::Color PaintColor = sf::Color(255,255,255,255);
sf::SoundBufferRecorder Recorder;
sf::SoundBuffer SndBuffer;
sf::Sound Snd;
sf::Font MyFont;
sf::String Txt;
sf::Shape Pixel;
// SCREEN
void SF_Screen_New(int width, int height, const char* title)
{
App.Create(sf::VideoMode(width, height, 32), title);
App.PreserveOpenGLStates(true);
}
void SF_Screen_Set_XY(int x, int y)
{
App.SetPosition(x,y);
}
int SF_Screen_Exit()
{
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
return(-1);
}
}
void SF_Screen_Set_Size(int32 Width, int32 Height)
{
App.SetSize(Width,Height);
}
void SF_Screen_Hide()
{
App.Show(0);
}
void SF_Screen_Show()
{
App.Show(-1);
}
void SF_Screen_Set_Active()
{
App.SetActive();
}
void SF_Screen_CLS()
{
App.Clear(BackgroundColor);
}
void SF_Screen_Set_MAXFPS(int32 fps)
{
App.SetFramerateLimit(fps);
}
void SF_Screen_Close()
{
App.Close();
}
void SF_Screen_Set_Width(int Width)
{
int x = App.GetHeight();
App.SetSize(Width,x);
}
int SF_Screen_Get_Width()
{
return(App.GetWidth());
}
void SF_Screen_Set_Height(int Height)
{
int x = App.GetWidth();
App.SetSize(x,Height);
}
int SF_Screen_Get_Height()
{
return(App.GetHeight());
}
void SF_Screen_Display()
{
App.Display();
}
void SF_Screen_Set_Color(int r, int g, int b, int a)
{
sf::Color col = sf::Color(r,g,b,a);
BackgroundColor = col;
}
// MOUSE
unsigned int SF_Mouse_Get_X()
{
const sf::Input& Input = App.GetInput();
return(Input.GetMouseX());
}
unsigned int SF_Mouse_Get_Y()
{
const sf::Input& Input = App.GetInput();
return(Input.GetMouseY());
}
unsigned int SF_Mouse_Get_LB()
{
const sf::Input& Input = App.GetInput();
return(-Input.IsMouseButtonDown(sf::Mouse::Left));
}
unsigned int SF_Mouse_Get_RB()
{
const sf::Input& Input = App.GetInput();
return(-Input.IsMouseButtonDown(sf::Mouse::Right));
}
void SF_Mouse_Set_XY(int x, int y)
{
App.SetCursorPosition(x,y);
}
void SF_Mouse_Hide()
{
App.ShowMouseCursor(0);
}
void SF_Mouse_Show()
{
App.ShowMouseCursor(1);
}
//Font and Text
void SF_Font_Load(const char* Filename, int res)
{
MyFont.LoadFromFile(Filename, res);
}
void SF_Text_Print (int x, int y, const char* Text, int size)
{
Txt.SetText(Text);
Txt.SetFont(MyFont);
Txt.SetSize(size);
Txt.SetX(x);
Txt.SetY(y);
App.Draw(Txt);
}
void SF_Text_Set_Color (int r, int g, int b, int a)
{
sf::Color col = sf::Color(r,g,b,a);
Txt.SetColor(col);
}
void SF_Text_Set_Rotation(float Rot)
{
Txt.SetRotation(Rot);
}
void SF_Text_Set_Center(float x, float y)
{
Txt.SetCenter(x,y);
}
//Sound
void SF_Mic_Get_Input(int SampleRate)
{
Recorder.Start(SampleRate);
}
void SF_Mic_Stop_Input()
{
Recorder.Stop();
}
void SF_Mic_Play()
{
const sf::SoundBuffer& Buffer = Recorder.GetBuffer();
sf::Sound Sound(Buffer);
Sound.Play();
while (Sound.GetStatus() == sf::Sound::Playing){}
}
void SF_Mic_Save(const char* FileName)
{
const sf::SoundBuffer& Buffer = Recorder.GetBuffer();
sf::Sound Sound(Buffer);
Buffer.SaveToFile(FileName);
}
const sf::Int16 SF_Mic_Buffer_Load_SNDRAW(int SampleNum)
{
const sf::SoundBuffer& Buffer = Recorder.GetBuffer();
return(Buffer.GetSamples()[SampleNum]);
}
int SF_Mic_Buffer_Get_SampleRate()
{
return(Recorder.GetSampleRate());
}
int SF_Mic_Buffer_Get_SampleCount()
{
const sf::SoundBuffer& Buffer = Recorder.GetBuffer();
return(Buffer.GetSamplesCount());
}
int SF_Sound_Buffer_Load_FromFile(const char* FileName1)
{
if (SndBuffer.LoadFromFile(FileName1))
return(-1);
}
void SF_Sound_Buffer_Play()
{
Snd.SetBuffer(SndBuffer);
Snd.Play();
}
int SF_Sound_Buffer_Get_SampleCount()
{
return(SndBuffer.GetSamplesCount());
}
int SF_Sound_Buffer_Get_SampleRate()
{
return(SndBuffer.GetSampleRate());
}
bool SF_Sound_Buffer_Save(const char* FileName)
{
return(SndBuffer.SaveToFile(FileName));
}
const sf::Int16 SF_Sound_Buffer_Load_SNDRAW(int SampleNum)
{
return(SndBuffer.GetSamples()[SampleNum]);
}
// 2d drawing functions
void SF_Paint_Set_Color(int r, int g, int b, int a)
{
PaintColor = sf::Color(r, g, b, a);
}
void SF_Pixel_Set(int x, int y)
{
Pixel = sf::Shape::Rectangle(x, y, x+1, y+1, PaintColor);
App.Draw(Pixel);
}
void SF_Circle(int x, int y, int Radius)
{
Pixel = sf::Shape::Circle(x, y, Radius, PaintColor);
Pixel.EnableFill(true);
App.Draw(Pixel);
}
void SF_Circle_NoFill(int x, int y, int Radius)
{
Pixel = sf::Shape::Circle(x, y, Radius, PaintColor, 1, PaintColor);
Pixel.EnableFill(false);
Pixel.EnableOutline(true);
App.Draw(Pixel);
}
void SF_Rectangle(int x, int y, int width, int height)
{
Pixel = sf::Shape::Rectangle(x, y, x + width, y + height, PaintColor);
Pixel.EnableFill(true);
App.Draw(Pixel);
}
void SF_Rectangle_NoFill(int x, int y, int width, int height)
{
Pixel = sf::Shape::Rectangle(x, y, x + width, y + height, PaintColor, 1, PaintColor);
Pixel.EnableFill(false);
Pixel.EnableOutline(true);
App.Draw(Pixel);
}
void SF_Line(int x, int y, int xx, int yy, int thick)
{
Pixel = sf::Shape::Line(x, y, xx, yy, thick, PaintColor);
Pixel.EnableFill(true);
App.Draw(Pixel);
}
//GamePad input
bool SF_GamePad_Button_Get_State(int GamePad, int Button)
{
const sf::Input& Input = App.GetInput();
return(Input.IsJoystickButtonDown(GamePad, Button));
}
int SF_GamePad_Axis_Get_X (int GamePad)
{
const sf::Input& Input = App.GetInput();
return(Input.GetJoystickAxis(GamePad, sf::Joy::AxisX));
}
int SF_GamePad_Axis_Get_Y (int GamePad)
{
const sf::Input& Input = App.GetInput();
return(Input.GetJoystickAxis(GamePad, sf::Joy::AxisY));
}
int SF_GamePad_Axis_Get_Z (int GamePad)
{
const sf::Input& Input = App.GetInput();
return(Input.GetJoystickAxis(GamePad, sf::Joy::AxisZ));
}
int SF_GamePad_Axis_Get_R (int GamePad)
{
const sf::Input& Input = App.GetInput();
return(Input.GetJoystickAxis(GamePad, sf::Joy::AxisR));
}
int SF_GamePad_Axis_Get_U (int GamePad)
{
const sf::Input& Input = App.GetInput();
return(Input.GetJoystickAxis(GamePad, sf::Joy::AxisU));
}
int SF_GamePad_Axis_Get_V (int GamePad)
{
const sf::Input& Input = App.GetInput();
return(Input.GetJoystickAxis(GamePad, sf::Joy::AxisV));
}
int SF_GamePad_Axis_Get_POV (int GamePad)
{
const sf::Input& Input = App.GetInput();
return(Input.GetJoystickAxis(GamePad, sf::Joy::AxisPOV));
}
// Keyboard Input v.01
int SF_Key_Left()
{
int kval =0;
const sf::Input& Input = App.GetInput();
if (Input.IsKeyDown(sf::Key::Left))
kval = -1;
else
kval = 0;
return(kval);
}
int SF_Key_Right()
{
int kval =0;
const sf::Input& Input = App.GetInput();
if (Input.IsKeyDown(sf::Key::Right))
kval = -1;
else
kval = 0;
return(kval);
}
int SF_Key_Up()
{
int kval =0;
const sf::Input& Input = App.GetInput();
if (Input.IsKeyDown(sf::Key::Up))
kval = -1;
else
kval = 0;
return(kval);
}
int SF_Key_Down()
{
int kval =0;
const sf::Input& Input = App.GetInput();
if (Input.IsKeyDown(sf::Key::Down))
kval = -1;
else
kval = 0;
return(kval);
}
int SF_Key_A()
{
int kval =0;
const sf::Input& Input = App.GetInput();
if (Input.IsKeyDown(sf::Key::A))
kval = -1;
else
kval = 0;
return(kval);
}
int SF_Key_B()
{
int kval =0;
const sf::Input& Input = App.GetInput();
if (Input.IsKeyDown(sf::Key::B))
kval = -1;
else
kval = 0;
return(kval);
}
// Other Stuff
void SF_Push_Events()
{
sf::Event Event;
while (App.GetEvent(Event))
{ }
}