*небольшой рефакторинг

*добавлен метод encode_size для Frame
This commit is contained in:
K1ndWha1e committed 2026-07-12 00:26:27 +03:00
1 parent 81034325a1
commit 18ac12f3b5
6 files changed
+55 -60

No files matched your search

+1 -1
View File
@@ -1,3 +1,3 @@
data\
*.mp3
temp.v
path.text
+13 -3
View File
@@ -8,12 +8,13 @@ pub struct Frame {
size u64
start u64
end u64
apic ?[]u8
coded_size ?[]u64
addition []u8
encoding string
text string
name string
addition []u8
encoded [4]u8
apic ?[]u8
coded_size ?[]u64
}
// Во врейме первый байт указывает кодировку 0 - ISO-8859-1 (Latin-1), 1 - UTF-16LE
@@ -70,3 +71,12 @@ pub fn (mut frame Frame) encode_size(idv3_version u8) []u64 {
frame.size & stride[3]
]
}
@[inline]
pub fn encode_size(mut frame Frame) {
frame.encoded[3] = u8(frame.size & 0x7F)
frame.encoded[2] = u8((frame.size >> 7) & 0x7F)
frame.encoded[1] = u8((frame.size >> 14) & 0x7F)
frame.encoded[0] = u8((frame.size >> 21) & 0x7F)
}
+4 -23
View File
@@ -71,29 +71,14 @@ fn get_size_v23(size_bytes []u8) u64 {
return (u64(size_bytes[0]) << 24) | (u64(size_bytes[1]) << 16) | (u64(size_bytes[2]) << 8) | u64(size_bytes[3])
}
// Получения размеров всех тэгов
pub fn (mut id3v23 ID3v23) get_full_size(mut audio File) {
// Первые 10 байт тэга
first_ten_bytes := get_bytes(mut audio, 0, 10)
id3v23.size = (u64(first_ten_bytes[6]) << 21) | (u64(first_ten_bytes[7]) << 14) | (u64(first_ten_bytes[8]) << 7) | u64(first_ten_bytes[9])
get_full_size(mut id3v23, mut audio)
}
// Заполнение тэга
pub fn (mut id3v23 ID3v23) fill_tag(mut audio File) {
mut current_frame := &Frame{}
// Самая первая позиция
mut start_position := u64(10)
// Версия IDv3
idv3_version := u8(3)
// Получаем вне возможные байты
all_tag_bytes := get_bytes(mut audio, 10, int(id3v23.size + start_position))
for id3v23.size > start_position {
current_frame.fill_frame(all_tag_bytes, start_position, idv3_version)
start_position = current_frame.end
id3v23.size -= current_frame.size
id3v23.add_frame(current_frame)
}
fill_tag[ID3v23](mut id3v23, mut audio)
}
// Конвертировать ID3 v2.3 в ID3 v1
@@ -108,7 +93,3 @@ pub fn (id3v23 ID3v23) to_v1() ID3v1 {
comment: id3v23.comment.text
}
}
pub fn to_bytes () {
}
+5 -25
View File
@@ -59,34 +59,14 @@ fn get_size_v24(size_bytes []u8) u64 {
return (u64(size_bytes[0]) << 21) | (u64(size_bytes[1]) << 14) | (u64(size_bytes[2]) << 7) | u64(size_bytes[3])
}
pub fn (mut id3v23 ID3v24) get_full_size(mut audio File) {
// Первые 10 байт тэга
first_ten_bytes := get_bytes(mut audio, 0, 10)
id3v23.size = (u64(first_ten_bytes[6]) << 21) | (u64(first_ten_bytes[7]) << 14) | (u64(first_ten_bytes[8]) << 7) | u64(first_ten_bytes[9])
// Получения размеров всех тэгов
pub fn (mut id3v24 ID3v24) get_full_size(mut audio File) {
get_full_size(mut id3v24, mut audio)
}
// Заполнение тэга
pub fn (mut id3v24 ID3v24) fill_tag(mut audio File) {
mut current_frame := &Frame{}
// Самая первая позиция
mut start_position := u64(10)
// Версия IDv3
idv3_version := u8(4)
// Получаем вне возможные байты
all_tag_bytes := get_bytes(mut audio, 10, int(id3v24.size + start_position))
// TODO Подумать как правильно считать размер
// Скорее всего просто скадлывать байтики, если нуль - вырубать
for id3v24.size > start_position {
// Заполнение кадров
current_frame.fill_frame(all_tag_bytes, start_position, idv3_version)
start_position = current_frame.end
id3v24.size -= current_frame.size
// Добавление кадра
id3v24.add_frame(current_frame)
}
fill_tag[ID3v24](mut id3v24, mut audio)
}
// Конвертировать ID3 v2.3 в ID3 v1
+2
View File
@@ -29,6 +29,7 @@ pub struct ID3v23 {
apic Frame // Обложка
comment Frame // Комментарий
others []Frame
version u8 = 3 // Версия тэга
embedded bool // Присутвует ли тэг
}
@@ -47,6 +48,7 @@ pub struct ID3v24 {
apic Frame // Обложка
comment Frame // Комментарий
others []Frame
version u8 = 4 // Версия тэга
embedded bool // Присутвует ли тэг
}
+30 -8
View File
@@ -15,14 +15,13 @@ pub fn (mut audio Audio) detect_tags(mut data os.File) {
// ? --- Проверка версии ID3v23.3 ---
first_ten_bytes := get_bytes(mut data, 0, 4)
if first_ten_bytes[..3].bytestr() == "ID3" && first_ten_bytes[3] == 3 {
audio.id3v23.embedded = true
}
// ===============================
// ? --- Проверка версии ID3v23.4 ---
if first_ten_bytes[..3].bytestr() == "ID3" && first_ten_bytes[3] == 4 {
audio.id3v24.embedded = true
if first_ten_bytes[..3].bytestr() == "ID3" {
if first_ten_bytes[3] == 3 {
audio.id3v23.embedded = true
} else if first_ten_bytes[3] == 4 {
// Проверка версии ID3v23.4
audio.id3v24.embedded = true
}
}
// ===============================
}
@@ -42,3 +41,26 @@ pub fn get_bytes(mut audio os.File, position int, size int) []u8 {
return last_bytes
}
pub fn fill_tag[T](mut tag T, mut audio os.File) {
mut current_frame := &Frame{}
// Самая первая позиция
mut start_position := u64(10)
// Получаем вне возможные байты
all_tag_bytes := get_bytes(mut audio, 10, int(tag.size + start_position))
for tag.size > start_position {
current_frame.fill_frame(all_tag_bytes, start_position, tag.version)
start_position = current_frame.end
tag.size -= current_frame.size
tag.add_frame(current_frame)
}
}
pub fn get_full_size[T](mut tag T, mut audio os.File) {
// Первые 10 байт тэга
first_ten_bytes := get_bytes(mut audio, 0, 10)
tag.size = (u64(first_ten_bytes[6]) << 21) | (u64(first_ten_bytes[7]) << 14) | (u64(first_ten_bytes[8]) << 7) | u64(first_ten_bytes[9])
}