~aleteoryx/muditaos

ref: eeafb5f1be091a34446b842dbde1b7d54cc029a7 muditaos/module-audio/board/rt1051/SAIAudioDevice.cpp -rw-r--r-- 3.1 KiB
eeafb5f1 — Lefucjusz [MOS-783] Moved battery config file to '/user/data' 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "SAIAudioDevice.hpp"

#include <Audio/Stream.hpp>
#include <log/log.hpp>

#include <cmath>

using namespace audio;

SAIAudioDevice::SAIAudioDevice(I2S_Type *base, sai_edma_handle_t *rxHandle, sai_edma_handle_t *txHandle)
    : _base(base), rx(rxHandle), tx(txHandle)
{}

void SAIAudioDevice::initiateRxTransfer()
{
    audio::Stream::Span dataSpan;

    Source::_stream->reserve(dataSpan);
    LOG_DEBUG("Initiate rx transfer with %u bytes", dataSpan.dataSize);
    auto xfer = sai_transfer_t{.data = dataSpan.data, .dataSize = dataSpan.dataSize};
    SAI_TransferReceiveEDMA(_base, rx, &xfer);
}

void SAIAudioDevice::initiateTxTransfer()
{
    audio::Stream::Span dataToSend;
    Sink::_stream->peek(dataToSend);

    auto xfer = sai_transfer_t{.data = dataToSend.data, .dataSize = dataToSend.dataSize};
    SAI_TransferSendEDMA(_base, tx, &xfer);
}

void SAIAudioDevice::onDataSend()
{
    audio::Stream::Span dataSpan;

    if (!txEnabled || !isSinkConnected()) {
        return;
    }

    /// pop previous read and peek next
    Sink::_stream->consume();
    Sink::_stream->peek(dataSpan);
    scaleOutputVolume(dataSpan);

    sai_transfer_t xfer{.data = dataSpan.data, .dataSize = dataSpan.dataSize};
    SAI_TransferSendEDMA(_base, tx, &xfer);
}

void SAIAudioDevice::onDataReceive()
{
    audio::Stream::Span dataSpan;

    if (!rxEnabled || !isSourceConnected()) {
        return;
    }

    /// reserve space for the next read commiting previously reserved block before
    Source::_stream->commit();
    Source::_stream->reserve(dataSpan);

    sai_transfer_t xfer{.data = dataSpan.data, .dataSize = dataSpan.dataSize};
    SAI_TransferReceiveEDMA(_base, rx, &xfer);
}

void SAIAudioDevice::enableInput()
{
    if (!isSourceConnected()) {
        LOG_FATAL("No output stream connected!");
        return;
    }

    rxEnabled = true;

    /// initiate first read
    initiateRxTransfer();
}

void SAIAudioDevice::enableOutput()
{
    if (!isSinkConnected()) {
        LOG_FATAL("No input stream connected!");
        return;
    }

    txEnabled = true;

    /// initiate first write
    initiateTxTransfer();
}

void SAIAudioDevice::disableInput()
{
    rxEnabled = false;
}

void SAIAudioDevice::disableOutput()
{
    txEnabled = false;
}
AudioDevice::RetCode SAIAudioDevice::setOutputVolume(float vol)
{
    vol = std::clamp(vol, minVolume, maxVolume);
    /// Using y=x^2 function as an approximation seems very natural and has the most useful range
    /// For more info check: https://www.dr-lex.be/info-stuff/volumecontrols.html
    volumeFactor = std::pow(1.0f * (vol / maxVolume), 2);
    return AudioDevice::RetCode::Success;
}
void SAIAudioDevice::scaleOutputVolume(audio::Stream::Span &span)
{
    if (volumeFactor < 1.0) {
        const auto samples      = reinterpret_cast<std::int16_t *>(span.data);
        const auto samplesCount = samples + span.dataSize / 2;
        std::for_each(samples, samplesCount, [this](auto &sample) { sample *= volumeFactor; });
    }
}