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 duration for all formats #108

Open
wants to merge 2 commits into
base: master
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
91 changes: 33 additions & 58 deletions dsf.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ package tag
import (
"errors"
"io"
"time"
)

// ReadDSFTags reads DSF metadata from the io.ReadSeeker, returning the resulting
// ReadDSFMeta reads DSF metadata from the io.ReadSeeker, returning the resulting
// metadata in a Metadata implementation, or non-nil error if there was a problem.
// samples: http://www.2l.no/hires/index.html
func ReadDSFTags(r io.ReadSeeker) (Metadata, error) {
func ReadDSFMeta(r io.ReadSeeker) (Metadata, error) {
dsd, err := readString(r, 4)
if err != nil {
return nil, err
Expand All @@ -31,79 +32,53 @@ func ReadDSFTags(r io.ReadSeeker) (Metadata, error) {
return nil, err
}

_, err = r.Seek(int64(id3Pointer), io.SeekStart)
_, err = r.Seek(int64(28), io.SeekCurrent)
if err != nil {
return nil, err
}

id3, err := ReadID3v2Tags(r)
sampleRate, err := readUint32LittleEndian(r)
if err != nil {
return nil, err
}

return metadataDSF{id3}, nil
}

type metadataDSF struct {
id3 Metadata
}

func (m metadataDSF) Format() Format {
return m.id3.Format()
}

func (m metadataDSF) FileType() FileType {
return DSF
}

func (m metadataDSF) Title() string {
return m.id3.Title()
}

func (m metadataDSF) Album() string {
return m.id3.Album()
}

func (m metadataDSF) Artist() string {
return m.id3.Artist()
}

func (m metadataDSF) AlbumArtist() string {
return m.id3.AlbumArtist()
}

func (m metadataDSF) Composer() string {
return m.id3.Composer()
}
_, err = r.Seek(int64(4), io.SeekCurrent)
if err != nil {
return nil, err
}

func (m metadataDSF) Year() int {
return m.id3.Year()
}
sampleNum, err := readUint64LittleEndian(r)
if err != nil {
return nil, err
}

func (m metadataDSF) Genre() string {
return m.id3.Genre()
}
duration := time.Second * (time.Duration(sampleNum) / time.Duration(sampleRate))

func (m metadataDSF) Track() (int, int) {
return m.id3.Track()
}
_, err = r.Seek(int64(id3Pointer), io.SeekStart)
if err != nil {
return nil, err
}

func (m metadataDSF) Disc() (int, int) {
return m.id3.Disc()
}
id3, err := ReadID3v2Tags(r)
if err != nil {
return nil, err
}

func (m metadataDSF) Picture() *Picture {
return m.id3.Picture()
return metadataDSF{
metadataID3v2: id3,
duration: duration,
}, nil
}

func (m metadataDSF) Lyrics() string {
return m.id3.Lyrics()
type metadataDSF struct {
*metadataID3v2
duration time.Duration
}

func (m metadataDSF) Comment() string {
return m.id3.Comment()
func (m metadataDSF) FileType() FileType {
return DSF
}

func (m metadataDSF) Raw() map[string]interface{} {
return m.id3.Raw()
func (m metadataDSF) Duration() time.Duration {
return m.duration
}
43 changes: 37 additions & 6 deletions flac.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@ package tag

import (
"errors"
"fmt"
"io"
"time"
)

// blockType is a type which represents an enumeration of valid FLAC blocks
type blockType byte

// FLAC block types.
const (
// Stream Info Block 0
// Padding Block 1
// Application Block 2
// Seektable Block 3
// Cue Sheet Block 5
streamInfoBlock blockType = 0
vorbisCommentBlock blockType = 4
pictureBlock blockType = 6
)

// ReadFLACTags reads FLAC metadata from the io.ReadSeeker, returning the resulting
// ReadFLACMeta reads FLAC metadata from the io.ReadSeeker, returning the resulting
// metadata in a Metadata implementation, or non-nil error if there was a problem.
func ReadFLACTags(r io.ReadSeeker) (Metadata, error) {
func ReadFLACMeta(r io.ReadSeeker) (Metadata, error) {
flac, err := readString(r, 4)
if err != nil {
return nil, err
Expand All @@ -35,11 +37,11 @@ func ReadFLACTags(r io.ReadSeeker) (Metadata, error) {
}

m := &metadataFLAC{
newMetadataVorbis(),
metadataVorbis: newMetadataVorbis(),
}

for {
last, err := m.readFLACMetadataBlock(r)
last, err := m.readFLACBlock(r)
if err != nil {
return nil, err
}
Expand All @@ -53,9 +55,10 @@ func ReadFLACTags(r io.ReadSeeker) (Metadata, error) {

type metadataFLAC struct {
*metadataVorbis
duration time.Duration
}

func (m *metadataFLAC) readFLACMetadataBlock(r io.ReadSeeker) (last bool, err error) {
func (m *metadataFLAC) readFLACBlock(r io.ReadSeeker) (last bool, err error) {
blockHeader, err := readBytes(r, 1)
if err != nil {
return
Expand All @@ -78,12 +81,40 @@ func (m *metadataFLAC) readFLACMetadataBlock(r io.ReadSeeker) (last bool, err er
case pictureBlock:
err = m.readPictureBlock(r)

case streamInfoBlock:
err = m.readStreamingInfoBlock(r, blockLen)

default:
_, err = r.Seek(int64(blockLen), io.SeekCurrent)
}
return
}

func (m *metadataFLAC) readStreamingInfoBlock(r io.Reader, len int) error {
data := make([]byte, len)
if _, err := r.Read(data); err != nil {
return err
}

sampleRate, err := cutBits(data, 80, 20)
if err != nil {
return fmt.Errorf("reading sample rate: %w", err)
}

sampleNum, err := cutBits(data, 108, 36)
if err != nil {
return fmt.Errorf("reading sample number: %w", err)
}

m.duration = time.Second * (time.Duration(sampleNum) / time.Duration(sampleRate))

return nil
}

func (m *metadataFLAC) FileType() FileType {
return FLAC
}

func (m *metadataFLAC) Duration() time.Duration {
return time.Second
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/dhowden/tag

go 1.20
go 1.22

require github.com/dhowden/itl v0.0.0-20170329215456-9fbe21093131

Expand Down
6 changes: 5 additions & 1 deletion id3v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"strconv"
"strings"
"time"
)

// id3v1Genres is a list of genres as given in the ID3v1 specification.
Expand Down Expand Up @@ -42,7 +43,7 @@ var ErrNotID3v1 = errors.New("invalid ID3v1 header")

// ReadID3v1Tags reads ID3v1 tags from the io.ReadSeeker. Returns ErrNotID3v1
// if there are no ID3v1 tags, otherwise non-nil error if there was a problem.
func ReadID3v1Tags(r io.ReadSeeker) (Metadata, error) {
func ReadID3v1Tags(r io.ReadSeeker) (metadataID3v1, error) {
_, err := r.Seek(-128, io.SeekEnd)
if err != nil {
return nil, err
Expand Down Expand Up @@ -142,3 +143,6 @@ func (metadataID3v1) Disc() (int, int) { return 0, 0 }
func (m metadataID3v1) Picture() *Picture { return nil }
func (m metadataID3v1) Lyrics() string { return "" }
func (m metadataID3v1) Comment() string { return m["comment"].(string) }
func (m metadataID3v1) Duration() time.Duration {
return time.Second
}
9 changes: 6 additions & 3 deletions id3v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type id3v2Header struct {
Unsynchronisation bool
ExtendedHeader bool
Experimental bool
FooterPresent bool
Size uint
}

Expand Down Expand Up @@ -97,6 +98,7 @@ func readID3v2Header(r io.Reader) (h *id3v2Header, offset uint, err error) {
Unsynchronisation: getBit(b[2], 7),
ExtendedHeader: getBit(b[2], 6),
Experimental: getBit(b[2], 5),
FooterPresent: getBit(b[2], 4),
Size: uint(get7BitChunkedInt(b[3:7])),
}

Expand Down Expand Up @@ -425,7 +427,7 @@ func (r *unsynchroniser) Read(p []byte) (int, error) {

// ReadID3v2Tags parses ID3v2.{2,3,4} tags from the io.ReadSeeker into a Metadata, returning
// non-nil error on failure.
func ReadID3v2Tags(r io.ReadSeeker) (Metadata, error) {
func ReadID3v2Tags(r io.ReadSeeker) (*metadataID3v2, error) {
h, offset, err := readID3v2Header(r)
if err != nil {
return nil, err
Expand All @@ -440,12 +442,13 @@ func ReadID3v2Tags(r io.ReadSeeker) (Metadata, error) {
if err != nil {
return nil, err
}
return metadataID3v2{header: h, frames: f}, nil

return &metadataID3v2{header: h, frames: f}, nil
}

var id3v2genreRe = regexp.MustCompile(`(.*[^(]|.* |^)\(([0-9]+)\) *(.*)$`)

// id3v2genre parse a id3v2 genre tag and expand the numeric genres
// id3v2genre parse a id3v2 genre tag and expand the numeric genres
func id3v2genre(genre string) string {
c := true
for c {
Expand Down
Loading