~aleteoryx/muditaos

ref: a3e512a89b698b4d8a8f8046521c327ab2eec147 muditaos/module-audio/Audio/decoder/Decoder.cpp -rw-r--r-- 5.6 KiB
a3e512a8 — Marcin Smoczyński [EGD-6674] Change negotiation of audio block size 4 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <cstdio>
#include <Utils.hpp>
#include "Decoder.hpp"
#include "decoderMP3.hpp"
#include "decoderFLAC.hpp"
#include "decoderWAV.hpp"

#include "fileref.h"
#include "tag.h"
#include "tfilestream.h"

namespace audio
{
    Decoder::Decoder(const char *fileName)
        : filePath(fileName), workerBuffer(std::make_unique<int16_t[]>(workerBufferSize)), tag(std::make_unique<Tags>())
    {

        fd = std::fopen(fileName, "r");
        if (fd == NULL) {
            return;
        }

        std::fseek(fd, 0, SEEK_END);
        fileSize = std::ftell(fd);
        std::rewind(fd);
    }

    Decoder::~Decoder()
    {
        if (audioWorker) {
            audioWorker->close();
        }

        if (fd) {
            std::fclose(fd);
        }
    }

    std::unique_ptr<Tags> Decoder::fetchTags()
    {
        if (fd) {
            auto inPos = std::ftell(fd);
            std::rewind(fd);
            TagLib::FileStream fileStream(fd);
            TagLib::FileRef tagReader(&fileStream);
            if (!tagReader.isNull() && tagReader.tag()) {
                TagLib::Tag *tags                   = tagReader.tag();
                TagLib::AudioProperties *properties = tagReader.audioProperties();

                tag->title  = tags->title().to8Bit();
                tag->artist = tags->artist().to8Bit();
                tag->album  = tags->album().to8Bit();
                tag->genre  = tags->genre().to8Bit();
                tag->year   = std::to_string(tags->year());

                tag->total_duration_s = properties->length();
                tag->duration_min     = tag->total_duration_s / utils::secondsInMinute;
                tag->duration_hour    = tag->duration_min / utils::secondsInMinute;
                tag->duration_sec     = tag->total_duration_s % utils::secondsInMinute;
                tag->sample_rate      = properties->sampleRate();
                tag->num_channel      = properties->channels();
                tag->bitrate          = properties->bitrate();
            }
            std::rewind(fd);
            fetchTagsSpecific();
            std::fseek(fd, inPos, SEEK_SET);
        }

        tag->filePath.append(filePath);
        // If title tag empty fill it with raw file name
        if (tag->title.size() == 0) {
            if (const auto pos = filePath.rfind("/"); pos == std::string::npos) {
                tag->title.append(filePath);
            }
            else {
                tag->title.append(&filePath[pos + 1]);
            }
        }
        return std::make_unique<Tags>(*tag);
    }

    std::unique_ptr<Decoder> Decoder::Create(const char *file)
    {
        std::unique_ptr<Decoder> dec;
        if ((strstr(file, ".wav") != NULL) || (strstr(file, ".WAV") != NULL)) {
            dec = std::make_unique<decoderWAV>(file);
        }
        else if ((strstr(file, ".mp3") != NULL) || (strstr(file, ".MP3") != NULL)) {
            dec = std::make_unique<decoderMP3>(file);
        }
        else if ((strstr(file, ".flac") != NULL) || (strstr(file, ".FLAC") != NULL)) {
            dec = std::make_unique<decoderFLAC>(file);
        }
        else {
            return nullptr;
        }

        if (!dec->isInitialized) {
            return nullptr;
        }
        else {
            return dec;
        }
    }

    void Decoder::convertmono2stereo(int16_t *pcm, uint32_t samplecount)
    {
        uint32_t i = 0, j = 0;

        memset(workerBuffer.get(), 0, workerBufferSize * sizeof(int16_t));

        for (; j < samplecount; j++) {
            workerBuffer[i++] = pcm[j];
            workerBuffer[i++] = pcm[j];
        }

        memcpy(pcm, &workerBuffer[0], samplecount * 2 * sizeof(int16_t));
    }

    void Decoder::startDecodingWorker(DecoderWorker::EndOfFileCallback endOfFileCallback)
    {
        assert(_stream != nullptr);
        if (!audioWorker) {
            audioWorker =
                std::make_unique<DecoderWorker>(_stream,
                                                this,
                                                endOfFileCallback,
                                                tag->num_channel == 1 ? DecoderWorker::ChannelMode::ForceStereo
                                                                      : DecoderWorker::ChannelMode::NoConversion);
            audioWorker->init();
            audioWorker->run();
        }
        else {
            LOG_DEBUG("AudioWorker already running.");
        }
    }

    void Decoder::stopDecodingWorker()
    {
        if (audioWorker) {
            audioWorker->close();
        }
        audioWorker = nullptr;
    }

    void Decoder::onDataReceive()
    {
        audioWorker->enablePlayback();
    }

    void Decoder::enableInput()
    {
        audioWorker->enablePlayback();
    }

    void Decoder::disableInput()
    {
        audioWorker->disablePlayback();
    }

    auto Decoder::getSourceFormat() -> AudioFormat
    {
        auto tags     = fetchTags();
        auto bitWidth = getBitWidth();
        // this is a decoder mono to stereo hack, will be removed when proper
        // transcoding implementation is added
        auto channels = tags->num_channel == 1 ? 2U : tags->num_channel;

        return AudioFormat{tags->sample_rate, bitWidth, channels};
    }

    auto Decoder::getSupportedFormats() -> const std::vector<AudioFormat> &
    {
        if (formats.empty()) {
            formats.push_back(getSourceFormat());
        }

        return formats;
    }

    auto Decoder::getTraits() const -> Endpoint::Traits
    {
        return Endpoint::Traits{};
    }

} // namespace audio