Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Getting the actual data in SoundBuffer?  (Read 6499 times)

0 Members and 1 Guest are viewing this topic.

Unseen Machine

  • Newbie
  • *
  • Posts: 8
    • View Profile
Getting the actual data in SoundBuffer?
« 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Getting the actual data in SoundBuffer?
« Reply #1 on: April 08, 2011, 08:53:44 pm »
Quote
How do i access the values of each sample in a SoundBuffer?

GetSamples().

Quote
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 ;)
Laurent Gomila - SFML developer

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Getting the actual data in SoundBuffer?
« Reply #2 on: April 08, 2011, 09:19:11 pm »
Quote from: "Laurent"
By the way, everything's explained in the documentation ;)


That should be in your signature  :lol:
{much better code}

Unseen Machine

  • Newbie
  • *
  • Posts: 8
    • View Profile
Yeh, but
« Reply #3 on: April 08, 2011, 09:37:01 pm »
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

Unseen Machine

  • Newbie
  • *
  • Posts: 8
    • View Profile
A differant explanation
« Reply #4 on: April 09, 2011, 12:35:03 am »
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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Getting the actual data in SoundBuffer?
« Reply #5 on: April 09, 2011, 09:31:56 am »
The pointer returned by GetSamples() points at the array of samples, so...
Code: [Select]
buffer.GetSamples()[SampleNum]
Laurent Gomila - SFML developer

Unseen Machine

  • Newbie
  • *
  • Posts: 8
    • View Profile
Thanks
« Reply #6 on: April 09, 2011, 04:17:23 pm »
I will look into it and report back later. SFML kicks SDL's butt  by the way..

Unseen Machine

  • Newbie
  • *
  • Posts: 8
    • View Profile
Superb!
« Reply #7 on: April 09, 2011, 04:42:01 pm »
Works like a dream, many thanks.

John

Unseen Machine

  • Newbie
  • *
  • Posts: 8
    • View Profile
Whats the limit?
« Reply #8 on: April 09, 2011, 10:43:15 pm »
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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Getting the actual data in SoundBuffer?
« Reply #9 on: April 09, 2011, 11:10:39 pm »
Quote
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.

Quote
Is there a Filesize limit?

No.

Quote
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.

Quote
Thanks and sorry for being a pest

I think you should write code instead of asking every possible question that comes to your mind ;)
Laurent Gomila - SFML developer

Unseen Machine

  • Newbie
  • *
  • Posts: 8
    • View Profile
Oh, you want code do ya!
« Reply #10 on: April 10, 2011, 12:39:35 am »
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.

Code: [Select]
//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))
{ }
}