Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Implementing an Audio Mixer, Part 1

Basic DSP with Qt Multimedia

Image Source: Wikipedia

Description from Wikipedia: Sampling and quantization of a signal (red) for 4-bit LPCM over a time domain at specific frequency.

Image Source: Wikimedia Commons

Description from Wikimedia Commons: Image of a discrete time sinusoid

xWave 1 (y1​)Wave 2 (y2​)Sum (y1+y2​)Clamped Sum
0+60−100−40-40
1−120+80−40−40
2+40+70+110+110
3−110−100−210−128
4+50−110−60−60
5−100+60−40−40
6+70+50+120+120
7−120−120−240−128
8+80−100−20−20
9−80+40−40−40
10+90+80+170+127
11−100−90−190−128
12+60−120−60−60
13−120+70−50−50
14+80−120−40−40
15−110+80−30−30
16+90−100−10−10
17−110+90−20−20
18+100−110−10−10
19−120−120−240−128
qint16 combineSamples(qint32 samp1, qint32 samp2)
{
    const auto sum = samp1 + samp2;

    if (std::numeric_limits<qint16>::max() < sum)
        return std::numeric_limits<qint16>::max();

    if (std::numeric_limits<qint16>::min() > sum)
        return std::numeric_limits<qint16>::min();

    return sum;
}
qint16 accumulatedSum = 0;

for (auto *stream : mStreams)
{
    // call stream->read(char *, qint64)
    // to read a sample from the stream into streamSample
    qint16 streamSample;
    stream->read(reinterpret_cast<char *>(&streamSample), sizeof(qint16)));

    // accumulate
    accumulatedSum = combineSamples(sample, accumulatedSum);
}
void readData(char *data, qint64 maxSize);
constexpr qint16 bitDepth = sizeof(qint16);
const qint16 numSamples = maxSize / bitDepth;
void readData(char *data, qint64 maxSize)
{
    // start with 0 in the buffer
    memset(data, 0, maxSize);

    constexpr qint16 bitDepth = sizeof(qint16);
    const qint16 numSamples = maxSize / bitDepth;

    for (auto *stream : mStreams)
    {
        // this pointer will be incremented across the buffer
        auto *cursor = reinterpret_cast<qint16 *>(data);
        qint16 sample;

        for (int i = 0; i < numSamples; ++i, ++cursor)
            if (stream->read(reinterpret_cast<char *>(&sample), bitDepth))
                *cursor = combineSamples(sample, *cursor);
    }
}

About KDAB


1 Comment

25 - Aug - 2024

nyanpasu64