~aleteoryx/muditaos

ref: d5f84437cab54acb9fc0b8f96deb6f5d46fed3de muditaos/module-audio/Audio/decoder/decoderMP3.cpp -rw-r--r-- 2.1 KiB
d5f84437 — Lefucjusz [MOS-92] Fix continuing music playback after BT disconnection 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#define DR_MP3_IMPLEMENTATION
#define DR_MP3_NO_STDIO

#include "decoderMP3.hpp"

#include <array>
#include <cstdio>

namespace audio
{

    decoderMP3::decoderMP3(const char *fileName) : Decoder(fileName)
    {

        if (fileSize == 0) {
            return;
        }

        mp3 = std::make_unique<drmp3>();

        drmp3_init(mp3.get(), this->drmp3_read, this->drmp3_seek, this, NULL);
        if (mp3 == NULL) {
            LOG_ERROR("********************************** Unable to init mp3 decoder");
            return;
            // Failed to open file
        }

        chanNumber = mp3->channels;
        sampleRate = mp3->sampleRate;
        // NOTE: Always convert to S16LE as internal format
        bitsPerSample = 16;
        isInitialized = true;
    }

    decoderMP3::~decoderMP3()
    {
        drmp3_uninit(mp3.get());
    }
    void decoderMP3::setPosition(float pos)
    {
        auto totalFramesCount = drmp3_get_pcm_frame_count(mp3.get());
        drmp3_seek_to_pcm_frame(mp3.get(), totalFramesCount * pos);
        position = float(totalFramesCount) * pos / float(sampleRate);
    }

    uint32_t decoderMP3::decode(uint32_t samplesToRead, int16_t *pcmData)
    {

        uint32_t samplesRead = 0;
        samplesRead =
            drmp3_read_pcm_frames_s16(mp3.get(), samplesToRead / chanNumber, reinterpret_cast<drmp3_int16 *>(pcmData));

        if (samplesRead) {
            position += float(samplesRead) / float(sampleRate);
        }

        return samplesRead * chanNumber;
    }

    size_t decoderMP3::drmp3_read(void *pUserData, void *pBufferOut, size_t bytesToRead)
    {
        auto *userdata = reinterpret_cast<decoderMP3 *>(pUserData);
        return std::fread(pBufferOut, 1, bytesToRead, userdata->fd);
    }

    drmp3_bool32 decoderMP3::drmp3_seek(void *pUserData, int offset, drmp3_seek_origin origin)
    {
        auto *userdata = reinterpret_cast<decoderMP3 *>(pUserData);
        return !std::fseek(userdata->fd, offset, origin == drmp3_seek_origin_start ? SEEK_SET : SEEK_CUR);
    }

} // namespace audio