~aleteoryx/muditaos

ref: 0a0366bb7474f15400948d9209cdc19274ab0e5d muditaos/module-audio/Audio/decoder/decoder.hpp -rw-r--r-- 2.9 KiB
0a0366bb — Piotr Tanski [EGD-4267] Don't clean application windows if the application switch is not yet confirmed. (#1040) 5 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <stdint.h>
#include <string>
#include <memory>
#include <optional>
#include <cstring>

#include <vfs.hpp>

namespace audio
{

    struct Tags
    {

        /* Total audio duration in seconds */
        uint32_t total_duration_s = 0;
        /* Audio duration - hours part */
        uint32_t duration_hour = 0;
        /* Audio duration - minutes part */
        uint32_t duration_min = 0;
        /* Audio duration - seconds part */
        uint32_t duration_sec = 0;

        /* Sample rate */
        uint32_t sample_rate = 0;
        /* Number of channels */
        uint32_t num_channel = 0;
        /* bitrate */
        uint32_t bitrate = 0;

        std::string artist   = "";
        std::string genre    = "";
        std::string title    = "";
        std::string album    = "";
        std::string year     = "";
        std::string filePath = "";

        Tags()
        {}

        // Copy constructor
        Tags(const Tags &p2)
        {
            total_duration_s = p2.total_duration_s;
            duration_hour    = p2.duration_hour;
            duration_min     = p2.duration_min;
            duration_sec     = p2.duration_sec;
            sample_rate      = p2.sample_rate;
            num_channel      = p2.num_channel;
            artist           = p2.artist;
            genre            = p2.genre;
            title            = p2.title;
            album            = p2.album;
            year             = p2.year;
            filePath         = p2.filePath;
        }
    };

    class decoder
    {

      public:
        decoder(const char *fileName);

        virtual ~decoder();

        virtual uint32_t decode(uint32_t samplesToRead, int16_t *pcmData) = 0;

        std::unique_ptr<Tags> fetchTags();

        // Range 0 - 1
        virtual void setPosition(float pos) = 0;

        uint32_t getSampleRate()
        {
            return sampleRate;
        }

        uint32_t getChannelNumber()
        {
            return chanNumber;
        }

        float getCurrentPosition()
        {
            return position;
        }

        // Factory method
        static std::unique_ptr<decoder> Create(const char *file);

      protected:
        virtual void fetchTagsSpecific(){};

        void convertmono2stereo(int16_t *pcm, uint32_t samplecount);

        const uint32_t workerBufferSize = 1024 * 8;

        uint32_t sampleRate = 0;
        uint32_t chanNumber = 0;
        float position      = 0;
        vfs::FILE *fd       = nullptr;
        uint32_t fileSize   = 0;
        std::string filePath;

        // Worker buffer used for converting mono stream to stereo
        std::unique_ptr<int16_t[]> workerBuffer;
        std::unique_ptr<Tags> tag;
        bool isInitialized = false;
    };

} // namespace audio