| 1 | // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package taglib provides utilities for parsing audio tags in |
| 16 | // various formats. |
| 17 | package taglib |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "errors" |
| 22 | "fmt" |
| 23 | "io" |
| 24 | "time" |
| 25 | |
| 26 | "github.com/hjfreyer/taglib-go/taglib/id3" |
| 27 | ) |
| 28 | |
| 29 | // GenericTag is implemented by all the tag types in this project. It |
| 30 | // gives an incomplete view of the information in each tag type, but |
| 31 | // is good enough for most purposes. |
| 32 | type GenericTag interface { |
| 33 | Title() string |
| 34 | Artist() string |
| 35 | Album() string |
| 36 | Comment() string |
| 37 | Genre() string |
| 38 | Year() time.Time |
| 39 | Track() uint32 |
| 40 | Disc() uint32 |
| 41 | |
| 42 | // CustomFrames returns non-standard, user-defined frames as a map from |
| 43 | // descriptions (e.g. "PERFORMER", "MusicBrainz Album Id", etc.) to |
| 44 | // values. |
| 45 | CustomFrames() map[string]string |
| 46 | |
| 47 | // TagSize returns the total size of the tag's header and frames, |
| 48 | // i.e. the position at which audio data starts. |
| 49 | TagSize() uint32 |
| 50 | } |
| 51 | |
| 52 | // Decode reads r and determines which tag format the data is in, if |
| 53 | // any, and calls the decoding function for that format. size |
| 54 | // indicates the total number of bytes accessible through r. |
| 55 | func Decode(r io.ReaderAt, size int64) (GenericTag, error) { |
| 56 | magic := make([]byte, 4) |
| 57 | if _, err := r.ReadAt(magic, 0); err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | |
| 61 | if !bytes.Equal(magic[:3], []byte("ID3")) { |
| 62 | return nil, errors.New("taglib: format not recognised (not ID3)") |
| 63 | } |
| 64 | |
| 65 | switch magic[3] { |
| 66 | case 2: |
| 67 | return nil, errors.New("taglib: format not supported (ID3 v2.2)") |
| 68 | case 3: |
| 69 | return id3.Decode23(r) |
| 70 | case 4: |
| 71 | return id3.Decode24(r) |
| 72 | default: |
| 73 | return nil, fmt.Errorf("taglib: format not supported (ID3 %d)", magic[3]) |
| 74 | } |
| 75 | } |