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

Author Topic: Minor problem in the tutorial and a question  (Read 3165 times)

0 Members and 1 Guest are viewing this topic.

Ripensis

  • Newbie
  • *
  • Posts: 3
    • View Profile
Minor problem in the tutorial and a question
« on: April 15, 2010, 07:19:01 am »
Hello guys,

 I want to congratulate you on a really great job regarding  this project, I love the way it is offers support through the tutorials and the forum.

I want to report a really minor problem: - the online tutorial for the audio streams is ok but in the downloadable source code there is a small typeO:

const sfInt16* Data = SoundData.GetSamples(); <-downloadable source code
 const sf::Int16* Data = SoundData.GetSamples();<-the online and good code
 
Now here goes my question:
I want, while i receive input from the microphone, to detect some changes in the input volume  and if possible the frequencies.
Can it be done?

I tried with GetVolume, GetPitch but it only returns the system values.
I belive this problem was discussed in the French forum but i do not speak French (i only understood part of it through Google Translate).

Thank you and keep up the good work.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Minor problem in the tutorial and a question
« Reply #1 on: April 15, 2010, 08:15:18 am »
Hi

Quote
I want to report a really minor problem: - the online tutorial for the audio streams is ok but in the downloadable source code there is a small typeO

Thanks for reporting it :)

Quote
I want, while i receive input from the microphone, to detect some changes in the input volume and if possible the frequencies.
Can it be done?

Yes. You have to write a custom SoundRecorder, this way you can handle the sound data in real time.
Laurent Gomila - SFML developer

Ripensis

  • Newbie
  • *
  • Posts: 3
    • View Profile
Minor problem in the tutorial and a question
« Reply #2 on: April 15, 2010, 09:20:19 am »
Thank you for the fast reply Laurent.

I will post here when i have a working code(hopefully :lol:).

Ripensis

  • Newbie
  • *
  • Posts: 3
    • View Profile
Minor problem in the tutorial and a question
« Reply #3 on: April 17, 2010, 09:34:23 am »
I finished the program, below you have the source code (maybe someone will find it useful).

There are two minor problems with it:
1)there is a small delay during the playback
2)if you change the Sleep numbers the program will not report accurate values for the Volume and Frequency.

Laurent I found the tutorials and you're hint very helpful:  Thank You :)

I used Visual C++ 2008 and it works :P (i tested it many times).

Code: [Select]


////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio.hpp>
#include <iomanip>
#include <cmath>
#include <iostream>


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
 

class MyCustomSoundRecorder : public sf::SoundRecorder
{
    virtual bool OnStart()
    {
       

        return true;
    }

    virtual bool OnProcessSamples(const sf::Int16* Samples, std::size_t SamplesCount)
    {

sf::SoundBuffer Buffer;                              
Buffer.LoadFromSamples(Samples,SamplesCount,1,44100); //used for microphone
// playing sound files - if you want to use this feature you must disable the entire
//                       Recorder class and also all the Recorder references in main()
//                       You will only leave the inside of the OnProcessSamples() function
// Buffer.LoadFromFile("pana2.wav");                
// const sf::Int16* Samples = Buffer.GetSamples();  
        //------->sStarting to play the Sound
sf::Sound Sound;
Sound.SetBuffer(Buffer);
Sound.Play();

sf::Sleep(0.1f); //small delay so that the Offset will be bigger than 0

//------->Variables for Volume and Frequency Calculation
int i,k,j=0,p=0;
float Volume=0.0,frecventa=0.0;

         
// Loop while the sound is playing
while (Sound.GetStatus() == sf::Sound::Playing)
{
//Sound.Pause();//Improves accuracy when playing files/does not work with the Recorder class
         
//------->Volume - The value range is between 0 and 32768(it almost never reaches 32000)
   Volume=0.0;      // For a x value on a scale 1 to 100 apply the formula x = (int)((100*Volume)/32768);
k=(int)(Sound.GetPlayingOffset()*Buffer.GetSampleRate());//current sample
for(i=j;i<k;i++)
Volume += Samples[i]*Samples[i];
Volume=sqrt(Volume/(k-j));//Value under 5 db are electrical noise(mute the mic if you want to see proof :D)
//j=k;  

//-------> Frequency
if (Samples[j]<0.0) p=0; else p=1;//0 - the first sample has a negative amplitude
 //1 - the first sample has a pozitive amplitude
frecventa=0.0;//initializing the frequency

for(i=j;i<k;i++)
{ //if a sign change occurs between two samples the frequency number increases
 //p variable remembers the sign of the Sample[i-1] (pozitive or negaative)
 if ((Samples[i]>0.0)&&(p==0)&&(Samples[i]>20.0)) {//20 is the value of trigger (so that no audio noise is taken in to account)
frecventa++;
p=1;
  }
 else
  if ((Samples[i]<0.0)&&(p==1)&&(Samples[i]<-20.0)) {
                                                frecventa++;
p=0;
                                                    };
}
j=k;//este de la volum;ptr ca sa imi usurez viata

//Sound.Play();//Improves accuracy when playing files/does not work with the Recorder class
     
// Leave some CPU time for other processes
        sf::Sleep(0.5f);

//Display Values  Volume Time Frequency
std::cout << "\rV T F " << std::fixed << std::setprecision(2) << Volume << " db   "<<Sound.GetPlayingOffset() << " sec   "<<frecventa <<" Hz   "<<std::endl;
}

        return true;
    }

    virtual void OnStop()
    {
       
    }
};




// Check that the device can capture audio
    if (sf::SoundRecorder::CanCapture() == false)
    {
        std::cout << "Sorry, audio capture is not supported by your system" << std::endl;
        return EXIT_SUCCESS;
    }
 

  MyCustomSoundRecorder Recorder;

  Recorder.Start(44100);
 
  std::cout << "Recording... press enter to stop";
  std::cin.ignore(10000, '\n');
   
  Recorder.Stop();

    // Wait until the user presses 'enter' key
    std::cout << "Press enter to exit..." << std::endl;
    std::cin.ignore(10000, '\n');

    return EXIT_SUCCESS;
}


katrin

  • Newbie
  • *
  • Posts: 17
    • View Profile
thanx for posting
« Reply #4 on: July 19, 2011, 08:08:08 pm »
Quote from: "Ripensis"
I finished the program, below you have the source code (maybe someone will find it useful).



Hi, just to let you know: The code is very helpful! Thanx for posting it!!!!!  :D