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

Author Topic: Creating audio by pulling single samples  (Read 12625 times)

0 Members and 1 Guest are viewing this topic.

janos_

  • Newbie
  • *
  • Posts: 9
    • View Profile
Creating audio by pulling single samples
« on: July 26, 2021, 03:09:29 pm »
Hello,

I am trying to create an audio on the fly. I want a sound to be playing and while that happens new audio samples should be pulled from a function.
I have looked at the sf::SoundStream class. But this requires you to push back a few thousand new samples each time onGetData(Chunk& data) gets called. I want to provide only 1 sample. The function onGetData should look something like this:

sf::Int16 pullSample()
{
    return 1000 * std::sin(currentTime * ...);
}

I know that this requires with a sample rate of 44100, 44100 calls to that function each second. Is this still somehow possible or are there other work-arounds?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Creating audio by pulling single samples
« Reply #1 on: July 27, 2021, 07:43:34 am »
I guess you want to generate samples one by one to keep the "currentTime" variable accurate? This is not a good idea: the moments when onGetData is called, and when the samples are actually played, are unrelated. And no, providing samples one by one won't work anyway.

The samples times must be virtual, not synced to the real time. Increment time by 1/44100 at each new sample and you'll get a perfect sine wave. And now, you can generate as many samples as you want at each call.
Laurent Gomila - SFML developer

janos_

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Creating audio by pulling single samples
« Reply #2 on: July 27, 2021, 11:12:02 am »
Yep that's exactly what I wanted. I am now keeping track of a vriable virtualTime and increment it everytime I create a new sample by 1/sample_rate, just like you said.

I noticed when I create a new sf::Int16 array each time the function gets called, there is some noise between the sample chunks. But this problem stopped being a problem when making the array a member of the class and not a temporary. I guess this had something to do with the fact that a pointer is passed to the Chunk and the underlying data was released before it could be copied.

Thank you very much!
« Last Edit: July 27, 2021, 11:59:54 am by janos_ »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Creating audio by pulling single samples
« Reply #3 on: July 27, 2021, 11:29:07 am »
If the sample storage is destroyed when the function returns (and it is, if declared locally inside the function), stream receives only garbage (noise) from onGetData.
Laurent Gomila - SFML developer

janos_

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Creating audio by pulling single samples
« Reply #4 on: July 27, 2021, 12:01:04 pm »
Yeah thought so. Thanks again.

 

anything