~aleteoryx/muditaos

ref: 36b0228de2c847f7e0f8bf7a215539609c82ebcd muditaos/module-audio/Audio/decoder/DecoderWAV.cpp -rw-r--r-- 3.5 KiB
36b0228d — Lefucjusz [BH-2100] Fix crash when seeking in large MP3 file 11 months 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
// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "DecoderWAV.hpp"
#include "DecoderCommon.hpp"
#include <log/log.hpp>
#include <memory>

#define DR_WAV_IMPLEMENTATION
#define DR_MP3_NO_STDIO

#include <src/dr_wav.h>

namespace audio
{
    DecoderWAV::DecoderWAV(const std::string &filePath) : Decoder(filePath), wav(std::make_unique<drwav>())
    {
        if (fileSize == 0) {
            return;
        }

        if (drwav_init(wav.get(), drwavRead, drwavSeek, this, nullptr) == DRWAV_FALSE) {
            LOG_ERROR("Unable to init WAV decoder");
            return;
        }

        /* NOTE: Always convert to S16LE as an internal format */
        channelCount  = wav->channels;
        sampleRate    = wav->sampleRate;
        bitsPerSample = 16;
        isInitialized = true;
    }

    DecoderWAV::~DecoderWAV()
    {
        if (isInitialized) {
            drwav_uninit(wav.get());
        }
    }

    auto DecoderWAV::setPosition(float pos) -> void
    {
        if (!isInitialized) {
            LOG_ERROR("WAV decoder not initialized");
            return;
        }

        const auto frameToSeek = static_cast<std::uint64_t>(wav->totalPCMFrameCount * pos);
        const auto status      = drwav_seek_to_pcm_frame(wav.get(), frameToSeek);
        if (status == DRWAV_FALSE) {
            LOG_ERROR("Failed to seek to frame %" PRIu64 " in file '%s'!", frameToSeek, filePath.c_str());
        }

        /* Calculate new position */
        position = frameToSeek / static_cast<float>(sampleRate);
    }

    auto DecoderWAV::rewind() -> void
    {
        setPosition(0.0f);
    }

    auto DecoderWAV::decode(std::uint32_t samplesToRead, std::int16_t *pcmData) -> std::int32_t
    {
        if (!isInitialized) {
            LOG_ERROR("WAV decoder not initialized");
            return 0;
        }

        const auto framesRead = drwav_read_pcm_frames_s16(wav.get(), samplesToRead / channelCount, pcmData);
        if (framesRead > 0) {
            /* Calculate frame duration in seconds */
            position += static_cast<float>(framesRead) / static_cast<float>(sampleRate);
        }
        else if (!fileExists(fd)) {
            /* Unfortunately this second check of file existence is needed
             * to verify whether lack of new samples was caused by EOF or by
             * deletion of the file. */
            LOG_WARN("File '%s' was deleted during playback!", filePath.c_str());
            return fileDeletedRetCode;
        }
        return framesRead * channelCount;
    }

    auto DecoderWAV::drwavRead(void *pUserData, void *pBufferOut, std::size_t bytesToRead) -> std::size_t
    {
        const auto decoderContext = static_cast<DecoderWAV *>(pUserData);

        /* Check if the file exists - std::fread happily returns bytesToRead if
         * requested to read from deleted file, what causes decoding library
         * to enter infinite loop of reading. */
        if (!fileExists(decoderContext->fd)) {
            return 0;
        }
        return std::fread(pBufferOut, 1, bytesToRead, decoderContext->fd);
    }

    auto DecoderWAV::drwavSeek(void *pUserData, int offset, drwav_seek_origin origin) -> drwav_bool32
    {
        const auto decoderContext = static_cast<DecoderWAV *>(pUserData);
        const auto seekError =
            std::fseek(decoderContext->fd, offset, (origin == drwav_seek_origin_start) ? SEEK_SET : SEEK_CUR);
        return (seekError == 0) ? DRWAV_TRUE : DRWAV_FALSE;
    }
} // namespace audio