SFML community forums

Help => Audio => Topic started by: smith199910 on December 21, 2016, 05:27:24 pm

Title: Music from buffer
Post by: smith199910 on December 21, 2016, 05:27:24 pm
// SFML 1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <SFML\Audio.hpp>
#include <Windows.h>
#include <thread>
#include <iostream>
#include "MethodHeaders.h"
#include <string>
#include <string.h>

int main(int nNumberOfArgs, char *pszArgs[]) {
        //check if audio capture is available
        if (!sf::SoundBufferRecorder::isAvailable())
        {
                std::cout << "error: audio capture is not available on this system" << std::endl;
                std::cin.get();
        }
        else
        {
                // create the recorder
                HandleSamples sampler;
                std::vector<std::string> devices = sampler.getAvailableDevices();
                std::cout << !devices.empty() << std::endl;
                if (!(devices.empty())) {
                        HandleSamples sampler;
                        sampler.setDevice(devices.at(0));
                        sampler.start();
                        std::cout << "sampler has started" << std::endl;
                        Sleep(1000000);
                        sampler.stop();
                }
                else
                        // handle if no microphone can be detected
                {
                        std::cout << "error: There is no audio input device detected on this system" << std::endl;
                        std::cin.get();
                }
               
               
        }
}
void handlePitch(float pitch) {
        std::cout << pitch << std::endl;
}

 



//sampler.cpp
#include "stdafx.h"
#include <SFML\Audio.hpp>
#include <thread>
#include <iostream>
#include "MethodHeaders.h"
bool HandleSamples::onProcessSamples(const sf::Int16* samples, std::size_t sampleCount) {
        sf::Music music;
        music.openFromMemory(samples, sampleCount);
        float pitch = music.getPitch();
        music.play();
        handlePitch(pitch);

        return true;
}

This is the error I get.
Failed to open sound file from memory (format not supported)
Failed to play audio stream: sound parameters have not been initialized (call initialize() first)
 

How would I fix this?
Title: Re: Music from buffer
Post by: TCVM on December 21, 2016, 07:09:34 pm
You have "openal32.dll" in your working directory, right?
Title: Re: Music from buffer
Post by: Laurent on December 21, 2016, 07:52:43 pm
You should first explain what you're trying to do because your code doesn't make much sense.

You get this error because Music::openFromMemory wants an audio file in memory (the same you would have on disk, but stored in memory), not an array of audio samples.
Title: Re: Music from buffer
Post by: smith199910 on January 03, 2017, 03:46:37 pm
I do have "openal32.dll" in my working directory.
I am trying to process a constant audio stream from a microphone and get the pitch using music.
Is there a way to load the array of audio samples into music to detect the pitch?
Title: Re: Music from buffer
Post by: Laurent on January 03, 2017, 03:51:23 pm
sf::Music doesn't detect the pitch. You must write your own algorithm for that.
Title: Re: Music from buffer
Post by: Hapax on January 03, 2017, 04:34:51 pm
Just to clarify a little, "pitch" - as used by SFML - effectively means the playback speed multiplier. Default pitch is 1; a pitch of 2 will play back the sound at twice its original speed (and an octave higher) whereas a pitch of 0.5 will play back the sound at half its original speed (and an octave lower).

Also:
https://en.wikipedia.org/wiki/Pitch_detection_algorithm
https://en.wikipedia.org/wiki/Spectral_density_estimation