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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Foaly

Pages: 1 2 3 [4]
46
Graphics / Better Way to cycle through colors
« on: May 22, 2012, 12:19:21 am »
Is there a better / more efficient way to cycle through all colors than this?!

// setup
    sf::Color color;

//...
// update loop ( fTime is the elapsed frame time)    

    fRed = color.r;
    fGreen = color.g;
    fBlue = color.b;
   
    if (fRed == 255 && fGreen < 255 && fBlue == 0)
        color = sf::Color(255, fGreen + 25 * fTime, 0);
    else if (fRed > 0 && fGreen == 255 && fBlue == 0)
        color = sf::Color(fRed - 25 * fTime, 255, 0);
    else if (fRed == 0 && fGreen == 255 && fBlue < 255)
        color = sf::Color(0, 255, fBlue + 25 * fTime);
    else if (fRed == 0 && fGreen > 0 && fBlue == 255)
        color = sf::Color(0, fGreen - 25 * fTime, 255);
    else if (fRed < 255 && fGreen == 0 && fBlue == 255)
        color = sf::Color(fRed + 25 * fTime, 0, 255);
    else if (fRed == 255 && fGreen == 0 && fBlue > 0)
        color = sf::Color(255, 0, fBlue - 25 * fTime);

// now use the color somehow

47
Feature requests / Ability to chose sound input device
« on: April 27, 2012, 11:59:32 am »
Hello!
While playing around with the audio library I noticed that I couldn't change the device that inputs the audio data (i.e. microphone). My laptop has a build in mic, but I would rather use a better microphone, that I plug in the back.
I saw in https://github.com/SFML/SFML/blob/master/src/SFML/Audio/SoundRecorder.cpp#L81 that SFML always uses the default device. Could there be a function added that lets you chose the audio input device?

Thanks,
Foaly :)

48
Audio / Help Beat Detection
« on: April 27, 2012, 09:57:12 am »
Hello everybody!
As I already said in this post http://en.sfml-dev.org/forums/index.php?topic=7642.0 I wanted to make a simple Beat Detection using SFML. I used the algorithm I found here: http://www.flipcode.com/misc/BeatDetectionAlgorithms.pdf (to be precise I implemented the third version of the first algorithm, because I want to use it for techno and electronic music later anyway).
In the thread above you can see that it didn't quiet work with sf::Music, because I couldn't acces the Samples in time, so I tried using sf::SoundRecorder, because I wanted to use a direct input later anyway. I could get it to work and using a custom SoundStream I could even get it play the samples afterwards. It seems to work fairly good if I put a mic in front of a speaker. It doesn't work too too well when i plug a cable from an ipod into the mic input directly.

So now here is the thing. I am new to the whole concept of threads and mutexs and such, but I would like to use this code in a later project. I know there are a lot of very talented C++ programmers here, so would anybody mind to look at my code and tell me what they think about it and what to improve. I'm not quiet sure if i use the mutexes correctly or if the way I pass the buffers around is the most efficient one or if there is some other way of optimizing my code... Please have a look and tell me what you think.
Also does anyone have an idea why plugging in a cable into the mic input directly makes the code constantly detect a beat?

Thanks in advance,
Foaly :)



Here is my code:
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <vector>

sf::Mutex GlobalMutex;

class CustomRecorder : public sf::SoundRecorder
{
    std::vector<sf::Int16> m_Samples;
    std::size_t m_ChunckSize;

    virtual bool onStart() // optional
    {
        // Initialize whatever has to be done before the capture starts
        m_ChunckSize = 1024;

        // Return true to start playing
        return true;
    }

    virtual bool onProcessSamples(const sf::Int16* samples, std::size_t sampleCount)
    {
//        std::cout << "Recording: " << sampleCount << "Samples" << std::endl;

        sf::Lock Lock(GlobalMutex);
        std::copy(samples, samples + sampleCount, std::back_inserter(m_Samples));

        // Return true to continue playing
        return true;
    }

    virtual void onStop() // optional
    {
        // Clean up whatever has to be done after the capture ends
    }

public:

    // Fill the given buffer with 1024 samples  (if possible)
    bool FillBuffer(std::vector<sf::Int16>& VectorToFill)
    {
        if(m_Samples.size() >= m_ChunckSize)
        {
            sf::Lock Lock(GlobalMutex);
            VectorToFill.insert(VectorToFill.begin(), m_Samples.begin(), m_Samples.begin() + m_ChunckSize + 1);
            m_Samples.erase(m_Samples.begin(), m_Samples.begin() + m_ChunckSize + 1);
            return true;
        }
        return false;
    }

};

class CustomPlayer : public sf::SoundStream
{
private:

    sf::Mutex m_mutex;
    std::vector<sf::Int16> m_Samples;
    bool b_FirstTime;

    virtual bool onGetData(sf::SoundStream::Chunk& Data)
    {
        sf::Lock Lock(m_mutex);

        // Check if there is enough data to stream
        if (m_Samples.size() < 1024)
        {
            // Returning false means that we want to stop playing the stream
            return false;
        }

        if(b_FirstTime)
            b_FirstTime = false;
        else
            m_Samples.erase(m_Samples.begin(), m_Samples.begin() + 1024 + 1);

        // Fill the stream chunk with a pointer to the audio data and the number of samples to stream
        Data.samples   = &m_Samples[0];
        Data.sampleCount = 1024;

        return true;
    }

    virtual void onSeek(sf::Time timeOffset)
     {
         // Change the current position in the stream source
     }


public:

    CustomPlayer()
    {
        b_FirstTime = true;
        initialize(1, 44100);
    }

    void FillBuffer(std::vector<sf::Int16>& SourceVector)
    {
        sf::Lock Lock(m_mutex);
        m_Samples.insert(m_Samples.end(), SourceVector.begin(), SourceVector.end() + 1);
    };

};


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

    srand(time(NULL));

    sf::RenderWindow window(sf::VideoMode(500, 500), "SFML works!");
    sf::RectangleShape Block;
    Block.setSize(sf::Vector2f(20, 200));
    Block.setOrigin(10, 100);
    Block.setPosition(250, 250);
    Block.setFillColor(sf::Color::Green);
    Block.setScale(1, 2);

    sf::Clock Clock;
    float FrameTime;

    CustomRecorder Recorder;
    CustomPlayer Player;

    std::vector<sf::Int16> Buffer;

    // Create the energy history buffer and set it to zero
    std::vector<float> EnergyHistoryBuffer;
    EnergyHistoryBuffer.resize(43, 0.f);

    Recorder.start();

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close Event
            if (event.type == sf::Event::Closed)
                window.close();

            // Escape key pressed
            if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
                window.close();

        }

        FrameTime = Clock.getElapsedTime().asSeconds();
//        std::cout << "Framerate: " << 1.f / FrameTime << std::endl;
        Clock.restart();


        if(Recorder.FillBuffer(Buffer))
        {
            GlobalMutex.lock();

            // Compute instant energy
            float InstantEnergie = 0;
            for (std::vector<sf::Int16>::iterator itor = Buffer.begin(); itor < Buffer.end(); itor++)
            {
                InstantEnergie += ((*itor) / 32767.f) * ((*itor) / 32767.f);
            }

            // Compute average local energy
            float AverageLocalEnergySum = 0;
            for (std::vector<float>::iterator itor = EnergyHistoryBuffer.begin(); itor < EnergyHistoryBuffer.end(); itor++)
            {
                AverageLocalEnergySum += (*itor);
            }
            float AverageLocalEnergy = (1/43.f) * AverageLocalEnergySum;

            // Calculate the Variance of the last samples
            float VarianceSum = 0;
            for (std::vector<float>::iterator itor = EnergyHistoryBuffer.begin(); itor < EnergyHistoryBuffer.end(); itor++)
            {
                VarianceSum += ((*itor) - AverageLocalEnergy) * ((*itor) - AverageLocalEnergy);
            }
            float Variance = (1/43.f) * VarianceSum;

            // Calculate the Sesibility Constant
            float SensibilityConstant = (-0.0025714 * Variance) + 1.5142857;

            // Insert the Instant energy at the front of the history buffer and remove the last entry
            EnergyHistoryBuffer.insert(EnergyHistoryBuffer.begin(), InstantEnergie);
            EnergyHistoryBuffer.pop_back();

//            std::cout << "Instant Energy: " << InstantEnergie << " Local Energy: " << SensibilityConstant * AverageLocalEnergy << std::endl;
            // see if we have a beat
            if(InstantEnergie > SensibilityConstant * AverageLocalEnergy)
            {
//                std::cout << "We have a BEAT!"  << std::endl;
                Block.setScale(1, 2);//(rand()) / RAND_MAX * (0.4) + 1.8 );
            }

            Player.FillBuffer(Buffer);
            Player.play();
            Buffer.clear();

            GlobalMutex.unlock();
        }

//        Block.rotate(50 * FrameTime);
        if(Block.getScale().y > 0.8)
            Block.setScale(1, Block.getScale().y - 0.9 * FrameTime);

        window.clear();
        window.draw(Block);
        window.display();
    }

    Recorder.stop();

    return 0;
}

edit: maybe a little suggestion for the forum: what about a "spoiler" function or something like that? So that you only see the code when you click on it :)

49
Graphics / iTunes Effects
« on: April 24, 2012, 09:36:13 pm »
Hi everybody.
I have a question. The other day I looked at the iTunes Effects, which you can play while music plays (by pressing CTRL + T). They look really cool, but I wondered how they work. I couldn't figure any pattern or anything like that out. Does anybody have an idea how something like that is realized/done?
Thanks in advance,
Foaly

50
Audio / Accessing Musics Samples
« on: April 19, 2012, 06:05:51 pm »
Hello everybody,
my questions is if it is possible to access the samples of a music file in real time. I'm not sure if it's clear what I mean, so I'll explain what I want to do. I would like to get the samples of a music file currently being processed, so I can do some sound manipulation in real time (while it's being played). I know this is possible with SoundBuffers, but I want to use bigger files (i.e. SoundStreams / sf::Music). Is there any way I can do this with SFML? And if not is there any way I can do this using as much of SFML convenience as possible?
Thanks in advance :)
Foaly

I am on a Windows machine using the current SFML2 rc btw.

51
Audio / Sine Wave
« on: February 24, 2012, 08:06:29 am »
Hi everybody,
I have a little problem. I am trying to write a little program that creates simple Sounds like a Sine wave or a Square wave. As for now everything is going quiet well. The only thing is that the output is not as I expected. It sounds right, but when I went to a program like Audacity and compared my output to their Sinewave (same with the Squarewave respectively) I noticed that my sound is one wavelength to short. I am not quiet sure why. I think it might be a rounding error... Can somebody please tell me whats wrong.
Thanks in advance,
Foaly

Code: [Select]
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <vector>
#include <math.h>

#define sin_d(x) (sin((x)*M_PI/180))

int main()
{
    int SampleRate = 44100; // 44100 = CD-Quality
    int Frequency = 440; // 440 = A
    float Duration = 1; // in Seconds
    int LengthInSamples = SampleRate * Duration;


    sf::Int16 Samples[LengthInSamples];

    float dist = SampleRate / Frequency;
    int dist2 = dist / 2;
    int count = LengthInSamples / dist +1;

    for(int a = 0; a < count; a++)
    {
        for(int b = 0; b < dist; b++)
        {
            if(a * dist + b >= LengthInSamples)
            {
                break;
            }

//            //Sine Wave
//            Samples[a*int(dist)+b] = sin_d(b / dist * 360.f) * 32767;

//            // Square Wave
//            if(b < dist2)
//            {
//                Samples[a*int(dist) + b] = 32767;
//            }
//            else
//            {
//                Samples[a*int(dist) + b] = -32767;
//            }
        }
    }

    sf::SoundBuffer FinalBuffer;
    FinalBuffer.LoadFromSamples(Samples, LengthInSamples, 1, SampleRate);
    FinalBuffer.SaveToFile("output.wav");

    return 0;
}

52
Audio / Combining Soundbuffers
« on: February 06, 2012, 11:00:14 am »
Hello everybody!

I have a little problem and I am not quiet sure how to solve it. Hopefully someone can point me the right direction.
So i would like to "combine" two soundbuffers. Meaning i have two different soundbuffers and i want to combine them into one. My idea was that I use GetSamples() to get the samples of each buffer and then add them together into one array and then call the LoadFromMemory() member of a third buffer to finish up. So the real question is how do I add the two array together? Or is there a better method to solve the whole problem?
I hope someone can help me. Thanks in advance.
Foaly

53
Graphics / SIGTRAP after SetText()
« on: June 29, 2011, 08:32:03 pm »
Hello there,
I have a little problem here and I hope that somebody can help me. I have been working on a game for quiet some time now and everything seemed to work fine. I'm not home for a couple of weeks now, but I wanted to continue working on my game. So i brought all the files with me and set everything up on a different computer. The program compiles just fine. But when i tried to fire up the debugger a weird thing happened. The program just crashed out of nowhere at start up. All the debugger was saying was:
Code: [Select]
Program received signal SIGTRAP, Trace/breakpoint trap.
In ?? () ()
Debugger finished with status 0

Of course that is useless as could be. The call stack did show anything interesting either. So I tried different things, but none of them seemed to work at all. Then i had the idea to update my IDE (i am using Code::Blocks 10.05). So i downloaded the debugger nighly (26 June 2011 build (rev 7257)). I still got the same error, but this time the debugger messages were a little more detailed. This time the call stack looked like this:
Code: [Select]
#0 7C90120F ntdll!DbgUiConnectToDbg() (C:\WINDOWS\system32\ntdll.dll:??)
#1 7C96EE31 ntdll!RtlpNtMakeTemporaryKey() (C:\WINDOWS\system32\ntdll.dll:??)
#2 7C96F26E ntdll!RtlpNtMakeTemporaryKey() (C:\WINDOWS\system32\ntdll.dll:??)
#3 7C970456 ntdll!RtlpNtMakeTemporaryKey() (C:\WINDOWS\system32\ntdll.dll:??)
#4 7C94BAFC ntdll!LdrFindEntryForAddress() (C:\WINDOWS\system32\ntdll.dll:??)
#5 015E0000 ?? () (??:??)
#6 7C91A1BA ntdll!RtlpUnWaitCriticalSection() (C:\WINDOWS\system32\ntdll.dll:??)
#7 77C2C2DE msvcrt!free() (C:\WINDOWS\system32\msvcrt.dll:??)
#8 015E0000 ?? () (??:??)
#9 00A25CCD __gnu_cxx::new_allocator<char>::deallocate(this=0x22fd2b, __p=0x679e9c34 "") (d:/programmes/mingw-4.4/bin/../lib/gcc/mingw32/4.4.0/include/c++/ext/new_allocator.h:95)
#10 00A4771D std::basic_string<unsigned int, std::char_traits<unsigned int>, std::allocator<unsigned int> >::_Rep::_M_destroy(this=0x679e9c34, __a=...) (d:/programmes/mingw-4.4/bin/../lib/gcc/mingw32/4.4.0/include/c++/bits/basic_string.tcc:427)
#11 00A4777E std::basic_string<unsigned int, std::char_traits<unsigned int>, std::allocator<unsigned int> >::_Rep::_M_dispose(this=0x679e9c34, __a=...) (d:/programmes/mingw-4.4/bin/../lib/gcc/mingw32/4.4.0/include/c++/bits/basic_string.h:231)
#12 00A47B00 std::basic_string<unsigned int, std::char_traits<unsigned int>, std::allocator<unsigned int> >::assign(this=0x15e8ba0, __str=...) (d:/programmes/mingw-4.4/bin/../lib/gcc/mingw32/4.4.0/include/c++/bits/basic_string.tcc:251)
#13 00A47EEC std::basic_string<unsigned int, std::char_traits<unsigned int>, std::allocator<unsigned int> >::operator=(this=0x15e8ba0, __str=...) (d:/programmes/mingw-4.4/bin/../lib/gcc/mingw32/4.4.0/include/c++/bits/basic_string.h:506)
#14 00A250CC sf::Unicode::Text::operator=(this=0x15e8ba0) (../../include/SFML/System/Unicode.hpp:64)
#15 00983958 sf::String::SetText(this=0x15e8af4, Text=...) (D:\dev\sfml\sdk\SFML-1.6\src\SFML\Graphics\String.cpp:68)
#16 00409629 CMainMenu::Init(this=0x15e8880) (..\Src\MainMenu.cpp:9)

The part of the code that the error is referring too looks like this:
Code: [Select]
In the header file:
sf::String creditsHeader;

and in the .cpp file:
creditsHeader.SetFont(sf::Font::GetDefaultFont());
creditsHeader.SetText("Credits");
creditsHeader.SetSize(80);

So I am really lost on this one and I don't know anything more that I could try... I hope somebody can help me.

Here are some more information on the system and versions:
Windows XP SP3
SFML 1.6
gcc 4.5.2

54
General discussions / OpenGL linking errors
« on: August 17, 2010, 11:31:29 am »
Hey guys,
i am new to SFML and so far i found it awesome! But now i got a little problem. I'm trying to make a little game and it all worked pretty well. Then i tried to add some animations. So i used these animation classes of the wiki. After adding the pausableclock.hpp it compiled, but then i got a bunch of linking errors such as:  undefined reference to glbegin@4. They all point to some OpenGl functions. So my question is can anybody tell me how to link that? According to the tutorial a simple adding of the window library should do it. Some how it doesnt work so. i linked my libraries like that:
-lsfml-graphics-d
-lsfml-audio-d
-lsfml-window-d
-lsfml-system-d

So can somebody please help me?

Pages: 1 2 3 [4]
anything