Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Synchronised Lyrics SYLT support #64

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions common_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ var (
"Size": "TSIZ",
"ISRC": "TSRC",
"Software/Hardware and settings used for encoding": "TSSE",
"Year": "TYER",
"User defined text information frame": "TXXX",
"Unique file identifier": "UFID",
"Unsynchronised lyrics/text transcription": "USLT",
"Synchronised lyrics/text": "SYLT",
"Year": "TYER",
"User defined text information frame": "TXXX",
"Unique file identifier": "UFID",
"Unsynchronised lyrics/text transcription": "USLT",

// Just for convenience.
"Artist": "TPE1",
Expand Down Expand Up @@ -109,6 +110,7 @@ var (
"ISRC": "TSRC",
"Software/Hardware and settings used for encoding": "TSSE",
"Set subtitle": "TSST",
"Synchronised lyrics/text": "SYLT",
"User defined text information frame": "TXXX",
"Unique file identifier": "UFID",
"Unsynchronised lyrics/text transcription": "USLT",
Expand Down Expand Up @@ -139,6 +141,7 @@ var parsers = map[string]func(*bufReader) (Framer, error){
"APIC": parsePictureFrame,
"COMM": parseCommentFrame,
"POPM": parsePopularimeterFrame,
"SYLT": parseSynchronisedLyricsFrame,
"TXXX": parseUserDefinedTextFrame,
"UFID": parseUFIDFrame,
"USLT": parseUnsynchronisedLyricsFrame,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/bogem/id3v2
module github.com/tramhao/id3v2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It need to be reverted 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This by mistake?


go 1.13

Expand Down
136 changes: 136 additions & 0 deletions synchronised_lyrics_frame.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright 2016 Albert Nigmatzianov. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package id3v2

import (
// "bytes"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// "bytes"

"encoding/binary"
// "fmt"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// "fmt"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These shouldn't be left in here.

"io"
)

// SynchronisedLyricsFrame is used to work with SYLT frames.
// The information about how to add synchronised lyrics/text frame to tag
// you can see in the docs to tag.AddSynchronisedLyricsFrame function.
//
// You must choose a three-letter language code from
// ISO 639-2 code list: https://www.loc.gov/standards/iso639-2/php/code_list.php
type SynchronisedLyricsFrame struct {
Encoding Encoding
Language string
TimestampFormat byte
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TimestampFormat byte
TimestampFormat TimestampFormat

ContentType byte
ContentDescriptor string
SynchronizedTexts []SyncedText
}

type TimestampFormat int

const (
Unknown TimestampFormat = iota
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO TimestampFormat is redundant here. Just use byte

Suggested change
Unknown TimestampFormat = iota
Unknown byte = iota

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also prefix timestamp formats. Please also get rid of Unknown, I can't find such TimestampFormat in spec

const (
	SYLTAbsoluteMpegFramesTimestampFormat = iota + 1
	SYLTAbsoluteMillisecondsTimestampFormat
)

AbsoluteMpegFrames
AbsoluteMilliseconds
)

var (
ContentType = map[int]string{
0: "Other",
1: "Lyrics",
2: "Transcription",
3: "Movement",
4: "Events",
5: "Chord",
6: "Trivia",
7: "WebpageUrls",
8: "ImageUrls",
}
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also use iota here:

const (
	SYLTOtherContentType = iota
	SYLTLyricsContentType
	SYLTTextTranscriptionContentType
	SYLTMovementContentType
	SYLTEventsContentType
	SYLTChordContentType
	SYLTTriviaContentType
	SYLTWebpageURLsContentType
	SYLTImageURLsContentType
)


type SyncedText struct {
Text string
Timestamp uint32
}

func (sylf SynchronisedLyricsFrame) Size() int {
var s int
for _, v := range sylf.SynchronizedTexts {
s += encodedSize(v.Text, sylf.Encoding)
s += len(sylf.Encoding.TerminationBytes)
s += 4
}

return 1 + len(sylf.Language) + encodedSize(sylf.ContentDescriptor, sylf.Encoding) +
+len(sylf.Encoding.TerminationBytes) + s +
+1 + 1
}

func (sylf SynchronisedLyricsFrame) UniqueIdentifier() string {
return sylf.Language + sylf.ContentDescriptor
}

func (sy SyncedText) uintToBytes() []byte {
bs := make([]byte, 4)
binary.BigEndian.PutUint32(bs, sy.Timestamp)
return bs
}

func bytesToInt(timeStampBytes []byte) uint32 {
return binary.BigEndian.Uint32(timeStampBytes)
}

func (sylf SynchronisedLyricsFrame) WriteTo(w io.Writer) (n int64, err error) {
if len(sylf.Language) != 3 {
return n, ErrInvalidLanguageLength
}
return useBufWriter(w, func(bw *bufWriter) {
bw.WriteByte(sylf.Encoding.Key)
bw.WriteString(sylf.Language)
bw.WriteByte(sylf.TimestampFormat)
bw.WriteByte(sylf.ContentType)
bw.EncodeAndWriteText(sylf.ContentDescriptor, sylf.Encoding)
bw.Write(sylf.Encoding.TerminationBytes)
for _, v := range sylf.SynchronizedTexts {
bw.EncodeAndWriteText(v.Text, sylf.Encoding)
bw.Write(sylf.Encoding.TerminationBytes)
bw.Write(v.uintToBytes())
}
})
}

func parseSynchronisedLyricsFrame(br *bufReader) (Framer, error) {
encoding := getEncoding(br.ReadByte())
language := br.Next(3)
timestampFormat := br.ReadByte()
contentType := br.ReadByte()
contentDescriptor := br.ReadText(encoding)

if br.Err() != nil {
return nil, br.Err()
}

var s []SyncedText
for {
textLyric, err := br.readTillDelims(encoding.TerminationBytes)
if err != nil {
break
}
t := SyncedText{Text: decodeText(textLyric, encoding)}
br.Next(len(encoding.TerminationBytes))
timeStamp := br.Next(4)
timeStampUint := bytesToInt(timeStamp)
t.Timestamp = timeStampUint
s = append(s, t)
}
sylf := SynchronisedLyricsFrame{
Encoding: encoding,
Language: string(language),
TimestampFormat: timestampFormat,
ContentType: contentType,
ContentDescriptor: decodeText(contentDescriptor, encoding),
SynchronizedTexts: s,
}

return sylf, nil
}
6 changes: 6 additions & 0 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func (tag *Tag) AddUnsynchronisedLyricsFrame(uslf UnsynchronisedLyricsFrame) {
tag.AddFrame(tag.CommonID("Unsynchronised lyrics/text transcription"), uslf)
}

// AddSynchronisedLyricsFrame adds the unsynchronised lyrics/text frame
// to tag.
func (tag *Tag) AddSynchronisedLyricsFrame(sylf SynchronisedLyricsFrame) {
tag.AddFrame(tag.CommonID("Synchronised lyrics/text"), sylf)
}

// AddUserDefinedTextFrame adds the custom frame (TXXX) to tag.
func (tag *Tag) AddUserDefinedTextFrame(udtf UserDefinedTextFrame) {
tag.AddFrame(tag.CommonID("User defined text information frame"), udtf)
Expand Down
Binary file modified testdata/test.mp3
Binary file not shown.