-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusicBlock.h
173 lines (129 loc) · 4.28 KB
/
MusicBlock.h
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
//
// Created by Dima on 17.11.2023.
//
#include "filesystem"
#include "iostream"
#include <fstream>
#include "SDL3_mixer/SDL_mixer.h"
#include "chrono"
#include "UUID.h"
#include "stb_image.h"
#include <taglib/attachedpictureframe.h>
#include "taglib/tiostream.h"
#include <taglib/fileref.h>
#include <taglib/mpegfile.h>
#include <taglib/id3v2tag.h>
#include "taglib/mpegfile.h"
#include <cstdio>
#pragma once
class MusicBlock
{
std::string name;
std::filesystem::path path;
std::chrono::milliseconds duration {0};
const unsigned char* imagedata;
Mix_Music* object = nullptr;
MusicPlayer::UUID uuid;
public:
const MusicPlayer::UUID &GetUUID() const
{
return uuid;
}
void SetIDFromJSON(MusicPlayer::UUID newuuid)
{
uuid = newuuid;
}
public:
MusicBlock() = default;
Mix_Music* GetMus()
{
return object;
}
std::chrono::milliseconds GetTime()
{
return duration;
}
const std::string& GetName()
{
return name;
}
const std::filesystem::path& GetPath()
{
//лол, тут пути то и нет по факту
return path;
}
const unsigned char*& GetImage()
{
return imagedata;
}
MusicBlock(const std::filesystem::path& path)
{
this->path = path;
object = Mix_LoadMUS(path.string().c_str());
name = Mix_GetMusicTitle(object);
if(name.empty())
name = std::filesystem::path(path).filename().replace_extension("").string();
double time = Mix_MusicDuration(object);
duration = std::chrono::seconds (static_cast<int64_t>(time));
if (object == NULL) {
std::cerr << "Ошибка при загрузке MP3: " << Mix_GetError() << std::endl;
return;
}
TagLib::MPEG::File file(path.string().c_str());
if (file.isValid()) {
TagLib::ID3v2::Tag* id3v2tag = file.ID3v2Tag();
if (id3v2tag) {
TagLib::ID3v2::FrameList frames = id3v2tag->frameList("APIC");
if (!frames.isEmpty()) {
auto* picFrame = dynamic_cast<TagLib::ID3v2::AttachedPictureFrame*>(frames.front());
if (picFrame) {
// std::string coverPath = "C:\\Users\\Dima\\Desktop\\cover.jpg";
// std::ofstream imageFile(coverPath, std::ios::binary);
// imageFile.write((const char*)picFrame->picture().data(), picFrame->picture().size());
// imageFile.close();
imagedata = reinterpret_cast<const unsigned char*>(picFrame->picture().data());
} else {
std::cerr << "Не удалось преобразовать фрейм в AttachedPictureFrame." << std::endl;
}
} else {
std::cout << "Обложка не найдена." << std::endl;
}
}
} else {
std::cerr << "Не удалось прочитать метаданные." << std::endl;
}
std::cout << std::format("MusicBlock created. UUID: {0}; Name: {1}; Path: {2}; Duration {3} ms", static_cast<uint64_t>(uuid), name, path.string(), duration) << '\n';
}
MusicBlock(MusicBlock&& original)
: uuid(original.uuid)
{
name = std::move(original.name);
path = std::move(original.path);
object = original.object;
duration = original.duration;
original.object = nullptr;
}
MusicBlock& operator=(MusicBlock&& original)
{
name = std::move(original.name);
path = std::move(original.path);
if (object!= nullptr)
Mix_FreeMusic(object);
object = original.object;
original.object = nullptr;
duration = original.duration;
uuid = original.uuid;
return *this;
}
void ChangeName(std::string newName)
{
name = newName;
}
~MusicBlock()
{
if(object == nullptr)
return;
Mix_FreeMusic(object);
std::cout << std::format("MusicBlock Deleted. UUID: {0}; Name: {1}; Path: {2}; Duration {3} ms", static_cast<uint64_t>(uuid), name, path.string(), duration) << '\n';
}
};