kcat / alure

Alure is a utility library for OpenAL, providing a C++ API and managing common tasks that include file loading, caching, and streaming
zlib License
70 stars 20 forks source link

How to fill buffers with data not from files #29

Open ethindp opened 5 years ago

ethindp commented 5 years ago

I'm curious -- how would I go about generating sine waves without resorting to the OpenAL API? I'd like to generate sine waves (or audio synthesis via oscillators in general) with Alure/OpenAL but have no idea how to do that. I could use a custom decoder, but I have no idea how I'd specify the frequency of the wave, the length, etc., and allow it to be flexible, so I could create a wave with a given length and frequency whenever I needed. Is such a thing possible?

kcat commented 5 years ago

Create and use a custom decoder, like this:

class MyDecoder : public alure::Decoder {
    uint64_t mPos{0u};
    ALfloat mSineFrequency{0.0f};

public:
    MyDecoder(ALfloat freq) : mFrequency(freq) { }

    ALuint getFrequency() const noexcept override
    {
        /* Sample rate of the buffer or stream, not your sine wave. Could use the
         * device's native sample rate instead. */
        return 44100;
    }

    ChannelConfig getChannelConfig() const noexcept override
    {
        /* Mono if you want to position it in 3D, or you don't need stereo. */
        return alure::ChannelConfig::Mono;
    };

    SampleType getSampleType() const noexcept override
    {
        /* Int16 might be more compatible, but Float32 is usually easier for
         * synthesized audio. */
        return alure::SampleType::Float32;
    }

    /* This returns the total number of sample frames you want the buffer
     * or stream to be. If this returns 0, it can't be used for a plain buffer,
     * but can be used for an unending stream that constantly generates
     * new samples as it plays. */
    uint64_t getLength() const noexcept override { return ...; }

    /* Not applicable to a buffer, or non-seekable stream. */
    bool seek(uint64_t pos) noexcept override { return false; }

    /* Don't need to bother with this if you don't need special loop points. */
    std::pair<uint64_t,uint64_t> getLoopPoints() const noexcept override
    { return std::make_pair<uint64_t,uint64_t>(0u, 0u); }

    /* This writes the actual buffer data. Assuming you're starting from the "mPos"
     * sample position, write "count" samples to "ptr". Use "mSineFrequency" and
     * the buffer/stream sample rate reported above as needed. It won't ask for
     * more samples in total than the previously-specified length, if it was non-0. */
    ALuint read(ALvoid *ptr, ALuint count) noexcept override
    {
        auto dst = static_cast<ALfloat*>(ptr);

        /* Write to dst[0...count-1] for mono, [0...count*2 - 1] for stereo, etc. */

        /* Increment the current position in case it's called again for another
         * chunk, return how much was written. If this returns less than the
         * originally requested amount, the buffer or stream ends here. */
        mPos += count;
        return count;
    }
};

Then simply create the buffer once with the context, giving it the custom decoder with the desired frequency:

alure::Buffer MySineBuffer = context->createBufferFrom("my_sine_wave0",
    alure::MakeShared<MyDecoder>(MySineFreq));

then hold onto the buffer and use it like normal:

source->play(MySineBuffer);

If you want a different buffer with a different sine frequency, create a different one:

alure::Buffer MyOtherSineBuffer = context->createBufferFrom("my_sine_wave1",
    alure::MakeShared<MyDecoder>(MyOtherSineFreq));
[...]
source->play(MyOtherSineBuffer);

Or alternatively, give the decoder directly to a source to stream it without a cached buffer:

source->play(alure::MakeShared<MyDecoder>(MySineFreq), ...);

If you have any difficulty or questions, please ask.

ethindp commented 5 years ago

So, if I take your class model, and modify it to suit my needs, it might become something like (taking from your altonegen.c example and attempting to port it over): class SineWaveDecoder : alure::Decoder { uint64_t mPos = 0; ALfloat mSineFrequency = 0.0;

public: SineWaveDecoder(ALfloat freq) : mFrequency(freq) { }

ALuint getFrequency() const noexcept override {
    return alure::Context::GetCurrent().getDevice().getFrequency();
}

ChannelConfig getChannelConfig() const noexcept override {
    return alure::ChannelConfig::Mono;
}

SampleType getSampleType() const noexcept override {
    return alure::SampleType::Float32;
}

uint64_t getLength() const noexcept override { return 0; }

bool seek(uint64_t pos) noexcept override { return false; }

std::pair<uint64_t,uint64_t> getLoopPoints() const noexcept

override { return std::make_pair<uint64_t,uint64_t>(0u, 0u); }

ALuint read(ALvoid *ptr, ALuint count) noexcept override {
    auto dst = static_cast<ALfloat*>(ptr);

ALdouble smps_per_cycle = (ALdouble) alure::Context::GetCurrent().getDevice().getFrequency() / mSineFrequency; for (ALuint i = 0; i < count-1; i++) data[i] += (ALfloat)(std::sin(i/smps_per_cycle 2.0M_PI) * 1.0); mPos += count; return count; } }; Is that right?

On 3/30/19, kcat notifications@github.com wrote:

Create an use a custom decoder, like this:

class MyDecoder : alure::Decoder {
    uint64_t mPos{0u};
    ALfloat mSineFrequency{0.0f};

public:
    MyDecoder(ALfloat freq) : mFrequency(freq) { }

    ALuint getFrequency() const noexcept override
    {
        /* Sample rate of the buffer or stream, not your sine wave. Could
use the
         * device's native sample rate instead. */
        return 44100;
    }

    ChannelConfig getChannelConfig() const noexcept override
    {
        /* Mono if you want to position it in 3D, or you don't need stereo.
*/
        return alure::ChannelConfig::Mono;
    };

    SampleType getSampleType() const noexcept override
    {
        /* Int16 might be more compatible, but Float32 is usually easier
for
         * synthesized audio. */
        return alure::SampleType::Float32;
    }

    /* This returns the total number of sample frames you want the buffer
     * or stream to be. If this returns 0, it can't be used for a plain
buffer,
     * but can be used for an unending stream that constantly generates
     * new samples as it plays. */
    uint64_t getLength() const noexcept override { return ...; }

    /* Not applicable to a buffer, or non-seekable stream. */
    bool seek(uint64_t pos) noexcept override { return false; }

    /* Don't need to bother with this if you don't need special loop points.
*/
    std::pair<uint64_t,uint64_t> getLoopPoints() const noexcept override
    { return std::make_pair<uint64_t,uint64_t>(0u, 0u); }

    /* This writes the actual buffer data. Assuming you're starting from the
"mPos"
     * sample position, write "count" samples to "ptr". Use "mSineFrequency"
and
     * the buffer/stream sample rate reported above as needed. It won't ask
for
     * more samples in total than the previously-specified length, if it was
non-0. */
    ALuint read(ALvoid *ptr, ALuint count) noexcept override
    {
        auto dst = static_cast<ALfloat*>(ptr);

        /* Write to dst[0...count-1] for mono, [0...count*2 - 1] for stereo,
etc. */

        /* Increment the current position in case it's called again for
another
         * chunk, return how much was written. If this returns less than
the
         * originally requested amount, the buffer or stream ends here. */
        mPos += count;
        return count;
    }
};

Then simply create the buffer once with the context, giving it the custom decoder with the desired frequency:

alure::Buffer MySineBuffer = context->createBufferFrom("my_sine_wave0",
    alure::MakeShared<MyDecoder>(MySineFreq));

then hold onto the buffer and use it like normal:

source->play(MySineBuffer);

If you want a different buffer with a different sine frequency, create a different one:

alure::Buffer MyOtherSineBuffer =
context->createBufferFrom("my_sine_wave1",
    alure::MakeShared<MyDecoder>(MyOtherSineFreq));
[...]
source->play(MyOtherSineBuffer);

Or alternatively, give the decoder directly to a source to stream it without a cached buffer:

source->play(alure::MakeShared<MyDecoder>(MySineFreq), ...);

If you have any difficulty or questions, please ask.

-- You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/kcat/alure/issues/29#issuecomment-478272979

-- Signed, Ethin D. Probst

kcat commented 5 years ago

So, if I take your class model, and modify it to suit my needs, it might become something like (taking from your altonegen.c example and attempting to port it over):

Close. I left a typo in the constructor (sorry about that), so it should be:

    SineWaveDecoder(ALfloat freq) : mSineFrequency(freq) { }

The fill loop should also account for mPos, and fill the whole buffer:

    for (ALuint i = 0; i < count; i++)
        data[i] += (ALfloat)(std::sin((mPos+i)/smps_per_cycle * 2.0*M_PI) * 1.0);
lain3d commented 4 years ago

Hi, I have the similar issue that I need to fill a buffer with data. All I have is an id number that corresponds to each of my own songs/sounds in memory. It seems that this relies on me having a file or making my own decoder. I know my data needs to be decoded either as a mp3 or it is a wav though. I thought of replacing FileIOFactory but I haven't been able to get it to work properly so far.

McSinyx commented 4 years ago

All I have is an id number that corresponds to each of my own songs/sounds in memory.

Do you mean you only have pointers to loaded audio files in memory?

lain3d commented 4 years ago

no I have char* for each id and I also know their length in bytes.

McSinyx commented 4 years ago

I assume that the char memory is of the audios, that's what I meant by having them loaded. FileIOFactory as you mentioned is a more logical approach for this, what have you tried so far? If we're going in that direction, we might need a separate issue though, and please post your current code there.

lain3d commented 4 years ago

moved to issue #41

kcat commented 4 years ago

Hi, I have the similar issue that I need to fill a buffer with data. All I have is an id number that corresponds to each of my own songs/sounds in memory. It seems that this relies on me having a file or making my own decoder. I know my data needs to be decoded either as a mp3 or it is a wav though. I thought of replacing FileIOFactory but I haven't been able to get it to work properly so far.

If the audio's already decoded in memory, making a decoder like the above would work. Just instead of supplying a sine frequency and generating the sine wave in the read method, you supply a pointer to the memory, its length, and the audio format, then copy the data from that pointer in the read method. You would only replace the FileIOFactory if you want to use different I/O methods than the standard file functions (for instance if you want to do I/O through PhysFS or your own custom VFS), which isn't what you're doing here.

lain3d commented 4 years ago

First of all thanks for answering :)

The audio is not already decoded in memory. It is undecoded in memory.

ethindp commented 4 years ago

Just kind of curious: would it be possible (and trivial to do) to add a custom IO factory for network-based audio? For example, say I want to write a VoIP application. Is it possible to make Alure use custom IO factories for particular functions? Or would I need separate contexts for those (and is that even possible, to create multiple OpenAL contexts)?

On 3/7/20, lain3d notifications@github.com wrote:

The audio is not already decoded in memory. It is undecoded in memory.

-- You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/kcat/alure/issues/29#issuecomment-596134948

-- Signed, Ethin D. Probst

kcat commented 4 years ago

It depends on how exactly you get the audio. If it's just a continuous stream, as I suspect VoIP generally is, a custom decoder would make more sense, whereas an IO factory is basically for handling a virtual or non-standard file system interface.

With an IO factory, it needs to generate "file" handles that contain set data; it would need to have appropriate format headers in the appropriate places and be able to seek to skip over or read the same data multiple times. A custom decoder, on the other hand, only needs functions that report information about the audio (its format, etc), and a function to provide audio samples in the reported format.

A decoder can "run" pretty much endlessly as long as it can keep providing more samples on request, whereas a file from an IO factory needs to have static data like a file on disk would.

ethindp commented 4 years ago

what's the process for writing custom decoders, and then asking Alure to use those? And is there any documentation on this stuff? That would be nice to have.

On 3/7/20, kcat notifications@github.com wrote:

It depends on how exactly you get the audio. If it's just a continuous stream, as I suspect VoIP generally is, a custom decoder would make more sense, whereas an IO factory is basically for handling a virtual or non-standard file system interface.

With an IO factory, it needs to generate "file" handles that contain set data; it would need to have appropriate format headers in the appropriate places and be able to seek to skip over or read the same data multiple times. A custom decoder, on the other hand, only needs functions that report information about the audio (its format, etc), and a function to provide audio samples in the reported format.

A decoder can "run" pretty much endlessly as long as it can keep providing more samples on request, whereas a file from an IO factory needs to have static data like a file on disk would.

-- You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/kcat/alure/issues/29#issuecomment-596158290

-- Signed, Ethin D. Probst

kcat commented 4 years ago

To make a custom decoder, inherit from allure::Decoder and implement the functions. The class definition describes the functions to implement: https://github.com/kcat/alure/blob/master/include/AL/alure2.h#L1427

In the case of a continuous stream, you'd have something like

class MyStream final : public alure::Decoder {
    ...your needed data fields here...

public:
    MyStream(...your data parameters...)
    {
        ...set up your data fields here...
    }

    /* Return the format of the samples being given (see the ChannelConfig
     * and SampleType enums)
     */
    ALuint getFrequency() const noexcept override
    { return stream frequency; }
    alure::ChannelConfig getChannelConfig() const noexcept override
    { return channel config; }
    alure::SampleType getSampleType() const noexcept override
    { return sample type; }

    /* No defined length, no seeking, no looping. */
    uint64_t getLength() const noexcept override { return 0; }
    bool seek(uint64_t pos) noexcept override { return false; }
    std::pair<uint64_t,uint64_t> getLoopPoints() const noexcept override { return {}; }

    ALuint read(ALvoid *ptr, ALuint count) noexcept override
    {
       ... get 'count' samples from your audio input, write to ptr...
       return number of samples written to ptr;
    }
};

Then you create an instance of your custom decoder, and play it on a source:

auto stream = alure::MakeShared<MyStream>(...your data parameters...);
...
source.play(stream, 4096, 3);