Giter Site home page Giter Site logo

Comments (14)

kcat avatar kcat commented on July 17, 2024 1

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.

from alure.

kcat avatar kcat commented on July 17, 2024

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.

from alure.

ethindp avatar ethindp commented on July 17, 2024

from alure.

kcat avatar kcat commented on July 17, 2024

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);

from alure.

lain3d avatar lain3d commented on July 17, 2024

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.

from alure.

McSinyx avatar McSinyx commented on July 17, 2024

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?

from alure.

lain3d avatar lain3d commented on July 17, 2024

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

from alure.

McSinyx avatar McSinyx commented on July 17, 2024

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.

from alure.

lain3d avatar lain3d commented on July 17, 2024

moved to issue #41

from alure.

kcat avatar kcat commented on July 17, 2024

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.

from alure.

lain3d avatar lain3d commented on July 17, 2024

First of all thanks for answering :)

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

from alure.

ethindp avatar ethindp commented on July 17, 2024

from alure.

ethindp avatar ethindp commented on July 17, 2024

from alure.

kcat avatar kcat commented on July 17, 2024

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);

from alure.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.