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

Author Topic: Music from buffer  (Read 4303 times)

0 Members and 2 Guests are viewing this topic.

smith199910

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Music from buffer
« 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?

TCVM

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Music from buffer
« Reply #1 on: December 21, 2016, 07:09:34 pm »
You have "openal32.dll" in your working directory, right?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Music from buffer
« Reply #2 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.
Laurent Gomila - SFML developer

smith199910

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Music from buffer
« Reply #3 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Music from buffer
« Reply #4 on: January 03, 2017, 03:51:23 pm »
sf::Music doesn't detect the pitch. You must write your own algorithm for that.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Music from buffer
« Reply #5 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
« Last Edit: January 03, 2017, 04:36:45 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*