Skip to content

Commit d31ffc4

Browse files
committed
Added read(), removed AudioProperties.
The read() call asynchronously fetches read-only tags and audio properties and discards the FileRef. This makes AudioProperties a redundant and synchronous way to fetch audio properties. Since they are read only by default, unlike tags, it is getter to just extract them and stuff them into an object rather than have a backing C++ object.
1 parent da8ac19 commit d31ffc4

File tree

9 files changed

+184
-183
lines changed

9 files changed

+184
-183
lines changed

Diff for: README.md

+32-33
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,33 @@ The `examples` show usage.
5656

5757
## API
5858

59+
### read(path, callback)
60+
61+
The function you will most likely want to use. `callback` should have signature
62+
`callback(err, tag, audioProperties)` where `tag` and `audioProperties` are
63+
plain-old JavaScript objects. For the distinction between these and `Tag`, see
64+
`Tag` below.
65+
66+
tag can have the following fields. node-taglib currently supports only the
67+
fields common to all formats:
68+
69+
* title (string)
70+
* album (string)
71+
* comment (string)
72+
* artist (string)
73+
* track (string)
74+
* year (integer)
75+
* genre (string)
76+
77+
The following fields are available in audioProperties, all are integers:
78+
79+
* length
80+
* bitrate
81+
* sampleRate
82+
* channels
83+
84+
Writing audio properties is not supported.
85+
5986
### tag(path, callback)
6087

6188
Read the tag from the file at `path` _asynchronously_. The callback should have
@@ -75,16 +102,8 @@ errors occurred, throws an exception.
75102
**NOTE: A Tag object should *NOT* be created using `new`. Instead use `tag()`
76103
or `tagSync()`**
77104

78-
A Tag object allows access to all the meta-data fields. node-taglib currently
79-
supports only the fields common to all formats:
80-
81-
* title (string)
82-
* album (string)
83-
* comment (string)
84-
* artist (string)
85-
* track (string)
86-
* year (integer)
87-
* genre (string)
105+
A Tag object allows _read-write_ access to all the meta-data fields. For valid
106+
field names see `read()` above.
88107

89108
To get a value, simply access the field -- `tag.artist`.
90109

@@ -96,10 +115,9 @@ have to call `saveSync()`** to actually save the changes to the file on disc.
96115
Due to TagLib's design, every `Tag` object in memory has to keep its backing
97116
file descriptor open. If you are dealing with a large number of files, you will
98117
soon run into problems because operating systems impose limits on how many
99-
files a process can have open simultaneously. If you only want to read
100-
meta-data and not write it immediately, then **deep copy** the fields over to
101-
a plain JS object, then dispose the `Tag` object so that you can operate on
102-
more files.
118+
files a process can have open simultaneously. If you want to only read tags,
119+
use `read()` instead as it will immediately close the file after the tag is
120+
read.
103121

104122
### Tag.save(callback)
105123

@@ -116,25 +134,6 @@ Save any changes in the Tag meta-data to disk _synchronously_.
116134

117135
Returns whether the tag is empty or not.
118136

119-
### AudioProperties(path)
120-
121-
**NOTE: This will be replaced by a more functional API, similar to the tags API.**
122-
123-
Object to get the audio properties of file at `path`. Throws an exception on
124-
errors.
125-
126-
var ap = new taglib.AudioProperties('path');
127-
console.log("Bitrate", ap.bitrate);
128-
129-
The following fields are available:
130-
131-
* length
132-
* bitrate
133-
* sampleRate
134-
* channels
135-
136-
Writing audio properties is not supported.
137-
138137
### taglib.WITH_ASF
139138

140139
A boolean representing whether node-taglib supports ASF files. Depends on

Diff for: async.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ match.find(process.argv[2], {fileFilters: [isMp3]}, function(err, files) {
1111
var count = 0;
1212
console.log(files.length, "files");
1313
async.forEach(files, function(fn, cb) {
14-
taglib.tag(fn, function(err, tag) {
14+
taglib.read(fn, function(err, tag) {
1515
if (err) {
1616
console.log("ERROR");
1717
return cb(false);

Diff for: examples/simple.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var Taglib = require(__dirname+"/../taglib")
22
var util = require("util")
33

44
for (var i = 2; i < process.argv.length; i++) {
5-
Taglib.tag(process.argv[i], function(err, tag) {
6-
console.dir(err ? err : tag);
5+
Taglib.read(process.argv[i], function(err, tag, props) {
6+
console.dir(err ? err : {'tag': tag, 'audioProperties': props});
77
});
88
}

Diff for: spec/taglibSpec.js

+64-43
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,6 @@ vows.describe('taglib bindings')
8080
}
8181
},
8282

83-
'reading AudioProperties from non-existent file': {
84-
topic: function() {
85-
return function() {
86-
return new Taglib.AudioProperties('thisfileobviouslyshouldnot.exist');
87-
}
88-
},
89-
90-
'should throw an exception': function(topic) {
91-
assert.throws(topic, /readable/);
92-
}
93-
},
94-
95-
'reading AudioProperties from a non-audio file': {
96-
topic: function() {
97-
return function() {
98-
return new Taglib.AudioProperties(__filename);
99-
}
100-
},
101-
102-
'should throw an exception': function(topic) {
103-
assert.throws(topic, /extract audio properties/);
104-
}
105-
},
106-
10783
'writing Tags to File': {
10884
topic: function() {
10985
var filename = __dirname+'/sample-write.mp3';
@@ -142,25 +118,6 @@ vows.describe('taglib bindings')
142118
}
143119
},
144120

145-
'reading Properties from File': {
146-
topic: new Taglib.AudioProperties(__dirname+'/blip.mp3'),
147-
'should be a `AudioProperties`': function (prop) {
148-
assert.equal(Taglib.AudioProperties, prop.constructor);
149-
},
150-
'should have length 1 second': function(prop) {
151-
assert.equal(1, prop.length);
152-
},
153-
'should have bitrate 128kbps': function(prop) {
154-
assert.equal(128, prop.bitrate);
155-
},
156-
'should have sampleRate 44100Hz': function(prop) {
157-
assert.equal(44100, prop.sampleRate);
158-
},
159-
'should have 2 channels': function(prop) {
160-
assert.equal(2, prop.channels);
161-
},
162-
},
163-
164121
'reading Tag from a file asynchronously': {
165122
topic: function() {
166123
Taglib.tag(__dirname+'/sample.mp3', this.callback);
@@ -210,5 +167,69 @@ vows.describe('taglib bindings')
210167
var tag = Taglib.tagSync(filename);
211168
assert.equal(tag.title, "Something completely different…");
212169
}
170+
},
171+
172+
'reading file metadata asynchronously': {
173+
topic: function() {
174+
Taglib.read(__dirname+'/sample.mp3', this.callback);
175+
},
176+
177+
'should be called with three arguments': function (err, tag, props) {
178+
assert.isNull(err);
179+
assert.isObject(tag);
180+
assert.isObject(props);
181+
},
182+
183+
'reading tags': {
184+
topic: function() {
185+
Taglib.read(__dirname+'/sample.mp3', this.callback);
186+
},
187+
188+
'title should be `A bit-bucket full of tags`': function (tag) {
189+
assert.equal(tag.title, 'A bit-bucket full of tags');
190+
},
191+
'artist should be by `gitzer\'s`': function (tag) {
192+
assert.equal(tag.artist, 'gitzer\'s');
193+
},
194+
'album should be on `Waffles for free!`': function (tag) {
195+
assert.equal(tag.album, "Waffles for free!");
196+
},
197+
'track should be the first': function (tag) {
198+
assert.equal(tag.track, 1);
199+
},
200+
'should be from 2011': function (tag) {
201+
assert.equal(tag.year, 2011);
202+
},
203+
'should have a silly comment': function(tag) {
204+
assert.equal(tag.comment, "Salami Wiglet.");
205+
}
206+
},
207+
208+
'reading audioProperties': {
209+
topic: function() {
210+
Taglib.read(__dirname+'/blip.mp3', this.callback);
211+
},
212+
213+
'should have length 1 second': function(err, _, prop) {
214+
assert.equal(prop.length, 1);
215+
},
216+
'should have bitrate 128kbps': function(err, _, prop) {
217+
assert.equal(prop.bitrate, 128);
218+
},
219+
'should have sampleRate 44100Hz': function(err, _, prop) {
220+
assert.equal(prop.sampleRate, 44100);
221+
},
222+
'should have 2 channels': function(err, _, prop) {
223+
assert.equal(prop.channels, 2);
224+
}
225+
},
226+
},
227+
228+
'read() on non-existent file': {
229+
topic: function() {
230+
return function() {
231+
Taglib.read('thisfileobviouslyshouldnot.exist', this.callback);
232+
}
233+
},
213234
}
214235
}).export(module);

Diff for: src/audioproperties.cc

-76
This file was deleted.

Diff for: src/audioproperties.h

-26
This file was deleted.

0 commit comments

Comments
 (0)