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

Author Topic: I am new to SFML how do i save buffers  (Read 3176 times)

0 Members and 1 Guest are viewing this topic.

smith199910

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
I am new to SFML how do i save buffers
« on: December 20, 2016, 05:10:47 pm »
This is the Difficult code:

// 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[])
{
       
        if (sf::SoundBufferRecorder::isAvailable())
        {
                std::vector<std::string> devices = sf::SoundRecorder::getAvailableDevices();
                // Record some audio data
                sf::SoundBufferRecorder recorder;
                if (devices.empty()) {
                        std::cout << "No available devices" << std::endl;
                }
                else
                {
                        std::cout << "program has started" << std::endl;
                recorder.setDevice(devices[0]);
                while(true)
                {
                recorder.start();
               
                        recorder.stop();
                // Get the buffer containing the captured audio data
                // this makes a reference while i want a copy // const sf::SoundBuffer& buffer = r     recorder.getBuffer();
                // sample input stream by copy for frequencies 1 - 8, each in its own thread
                // output frequency matches
                //save recording to buffer
                const sf::SoundBuffer buffer = recorder.getBuffer();
                //save buffer to file
                buffer.saveToFile("C:\\Users\\smithdus005\\Documents\\sample.ogg");
                std::thread sample(sampleAudio);
                std::thread play(playAudio);
                sample.join();
                play.join();
                }
 
                }
               
        }
  }
  void playAudio() {
        sf::Music music;
        //change into music format
        music.openFromFile("C:\\Users\\smithdus005\\Documents\\sample.ogg");
        //play music
        music.play();
  }
  void sampleAudio() {
        sf::Music music;
        //change into music format
        music.openFromFile("C:\\Users\\smithdus005\\Documents\\sample.ogg");
        //check pitch
        float currentPitch = music.getPitch();
  }


I get this error:
Failed to write ogg/vorbis file "C:\Users\smithdus005\Documents\sample.ogg" (unsupported bitrate)
« Last Edit: December 20, 2016, 05:12:28 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: I am new to SFML how do i save buffers
« Reply #1 on: December 20, 2016, 05:23:02 pm »
Please refrain from using special formatting where not needed. It only takes away from the readability and distracts from the content. Also make use of the [code=cpp][/code] tags.

What bitrate does your audio recorder, record at?
What values do you get, when you call getSampleRate() and getChannelCount() on the buffer?

Btw. you should always use forward slashes (/) they are supported on every system even Windows. Plus if you use relative paths, the code can work across platforms.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

smith199910

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: I am new to SFML how do i save buffers
« Reply #2 on: December 20, 2016, 05:36:47 pm »
I do not know what bit-rate it records at is there a  way to find that, or even better to set it.
When I call getSampleRate() and getChannelCount() on the buffer they both return 0 which seems wrong.

How would I use relative paths?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
I am new to SFML how do i save buffers
« Reply #3 on: December 20, 2016, 06:02:14 pm »
If you call getChannelCount and getSampleRate on the returned reference instead of the copy, what do you get?

Keep in mind that starting and stopping the recording one after the other will probably record nothing at all. Which might be what's showing an oversight in SFML, because we're not check if there actually is data to be written and juat try to save at which point the encoder will complain.
I will have to test thia scenario later today ir tomorrow.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/