aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.h
blob: 2f6b639c49ca2c5b4078e98272d768c067814dfb (plain)
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
#pragma once

#include <string>
#include <cmath>
#include <cstdint>
#include <cstddef>

#define NUMOF(l) (sizeof(l) / sizeof(*l))

#define linear_to_dB(x) (log10(x) * 20)

/*! Calculate the little string containing a bargraph
 * 'VU-meter' from the peak value measured
 */
const char* level(int channel, int peak);

size_t strlen_utf8(const char *s);

struct ICY_TEXT_t {
    std::string artist;
    std::string title;
    std::string now_playing;

    operator bool() const {
        return not (artist.empty() and title.empty() and now_playing.empty());
    }

    bool operator==(const ICY_TEXT_t& other) const {
        return
            artist == other.artist and
            title == other.title and
            now_playing == other.now_playing;
    }
    bool operator!=(const ICY_TEXT_t& other) const {
        return !(*this == other);
    }
    void useArtistTitle(const std::string& artist, const std::string& title) {
        this->artist = artist;
        this->title = title;
        now_playing = "";
    }
    void useNowPlaying(const std::string& now_playing) {
        artist = "";
        title = "";
        this->now_playing = now_playing;
    }
};

/*! Write the corresponding text to a file readable by ODR-PadEnc, with optional
 * DL+ information. The text is passed as a copy because we actually use the
 * m_nowplaying variable which is also accessed in another thread, so better
 * make a copy.
 *
 * \return false on failure
 */
bool write_icy_to_file(const ICY_TEXT_t text, const std::string& filename, bool dl_plus);