-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathalbum.cc
More file actions
228 lines (181 loc) · 7.31 KB
/
album.cc
File metadata and controls
228 lines (181 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* =====================================================================================
*
* Filename: album.cc
*
* Description: bindings for the album subsystem
*
* Version: 1.0
* Created: 03/01/2013 15:18:14
* Revision: none
* Compiler: gcc
*
* Author: Florent Jaby (FJ), florent.jaby@gmail.com
* Company: Florent Jaby
*
* =====================================================================================
*/
#include "common.h"
using namespace v8;
using namespace nsp;
/**
* JS album_is_loaded implementation. checks if a given album is loaded
*/
static Handle<Value> Album_Is_Loaded(const Arguments& args) {
HandleScope scope;
// test arguments sanity
assert(args.Length() == 1);
assert(args[0]->IsObject());
// gets sp_album pointer from given object
ObjectHandle<sp_album>* album = ObjectHandle<sp_album>::Unwrap(args[0]);
// actually call sp_album_is_loaded
bool loaded = sp_album_is_loaded(album->pointer);
return scope.Close(Boolean::New(loaded));
}
/**
* JS album_is_available implementation. checks if a given album is available
*/
static Handle<Value> Album_Is_Available(const Arguments& args) {
HandleScope scope;
// test arguments sanity
assert(args.Length() == 1);
assert(args[0]->IsObject());
// gets sp_album pointer from given object
ObjectHandle<sp_album>* album = ObjectHandle<sp_album>::Unwrap(args[0]);
// actually call sp_album_is_available
bool available = sp_album_is_available(album->pointer);
return scope.Close(Boolean::New(available));
}
/**
* JS album_name implementation. checks if a given album is loaded
*/
static Handle<Value> Album_Name(const Arguments& args) {
HandleScope scope;
// test arguments sanity
assert(args.Length() == 1);
assert(args[0]->IsObject());
// gets sp_album pointer from given object
ObjectHandle<sp_album>* album = ObjectHandle<sp_album>::Unwrap(args[0]);
const char* name = sp_album_name(album->pointer);
return scope.Close(String::New(name));
}
/**
* JS album_year implementation. checks if a given album is loaded
*/
static Handle<Value> Album_Year(const Arguments& args) {
HandleScope scope;
// test arguments sanity
assert(args.Length() == 1);
assert(args[0]->IsObject());
// gets sp_album pointer from given object
ObjectHandle<sp_album>* album = ObjectHandle<sp_album>::Unwrap(args[0]);
const int year = sp_album_year(album->pointer);
return scope.Close(Number::New(year));
}
/**
* JS album_type implementation. checks if a given album is loaded
*/
static Handle<Value> Album_Type(const Arguments& args) {
HandleScope scope;
// test arguments sanity
assert(args.Length() == 1);
assert(args[0]->IsObject());
// gets sp_album pointer from given object
ObjectHandle<sp_album>* album = ObjectHandle<sp_album>::Unwrap(args[0]);
sp_albumtype type = sp_album_type(album->pointer);
Handle<Value> res;
switch (type) {
case SP_ALBUMTYPE_ALBUM:
res = String::New("ALBUM");
break;
case SP_ALBUMTYPE_SINGLE:
res = String::New("SINGLE");
break;
case SP_ALBUMTYPE_COMPILATION:
res = String::New("COMPILATION");
break;
case SP_ALBUMTYPE_UNKNOWN:
default:
res = String::New("UNKNOWN");
break;
}
return scope.Close(res);
}
/**
* JS album_artist implementation. checks if a given album is loaded
*/
static Handle<Value> Album_Artist(const Arguments& args) {
HandleScope scope;
// test arguments sanity
assert(args.Length() == 1);
assert(args[0]->IsObject());
// gets sp_album pointer from given object
ObjectHandle<sp_album>* album = ObjectHandle<sp_album>::Unwrap(args[0]);
sp_artist* spartist = sp_album_artist(album->pointer);
ObjectHandle<sp_artist>* artist = new ObjectHandle<sp_artist>("sp_artist");
artist->pointer = spartist;
return scope.Close(artist->object);
}
/**
* Callback for sp_image_add_load_callback in Album_Cover().
* It calls the passed JS function callback and passes the raw image data as
* parameter as soon as the regarding image loading process has finished.
*/
void cb_image_loaded_album(sp_image *image, void *userdata) {
Persistent<Function> callback = static_cast<Function*>(userdata);
size_t image_size;
const void *image_data = sp_image_data(image, &image_size);
// Create a C++ world slow buffer:
node::Buffer *slowBuffer= node::Buffer::New(image_size);
memcpy(node::Buffer::Data(slowBuffer), image_data, image_size);
// Get the Buffer constructor from the JavaScript world:
Local<Object> globalObj = Context::GetCurrent()->Global();
Local<Function> bufferConstructor = Local<Function>::Cast(globalObj->Get(String::New("Buffer")));
Handle<Value> constructorArgs[3] = { slowBuffer->handle_, Integer::New(image_size), Integer::New(0) };
// Create a JavaScript buffer using the slow buffer:
Local<Object> actualBuffer = bufferConstructor->NewInstance(3, constructorArgs);
// Pass everything to the JavaScript callback:
const unsigned argc = 1;
Local<Value> argv[argc] = { actualBuffer };
callback->Call(callback, argc, argv);
// Clean up:
callback.Dispose();
sp_image_release(image);
}
/**
* JS album_cover implementation. Gets the albums image
*/
static Handle<Value> Album_Cover(const Arguments& args) {
HandleScope scope;
// test arguments sanity
assert(args.Length() == 4);
assert(args[0]->IsObject()); // sp_session
assert(args[1]->IsObject()); // sp_album
assert(args[2]->IsNumber()); // sp_image_size
assert(args[3]->IsFunction()); // callback after cover image was loaded
ObjectHandle<sp_session> *session = ObjectHandle<sp_session>::Unwrap(args[0]);
ObjectHandle<sp_album> *album = ObjectHandle<sp_album>::Unwrap(args[1]);
Handle<Integer> requestedImageSize = Local<Integer>::Cast(args[2]);
Handle<Function> callback = Persistent<Function>::New(Handle<Function>::Cast(args[3]));
sp_image_size imageSize = static_cast<sp_image_size>(requestedImageSize->Uint32Value());
const byte *imageId = sp_album_cover(album->pointer, imageSize);
if(imageId) {
sp_image *image = sp_image_create(session->pointer, imageId);
sp_image_add_load_callback(image, &cb_image_loaded_album, *callback);
return scope.Close(Boolean::New(true));
} else {
return scope.Close(Boolean::New(false));
}
}
void nsp::init_album(Handle<Object> target) {
NODE_SET_METHOD(target, "album_is_loaded", Album_Is_Loaded);
NODE_SET_METHOD(target, "album_is_available", Album_Is_Available);
NODE_SET_METHOD(target, "album_name", Album_Name);
NODE_SET_METHOD(target, "album_year", Album_Year);
NODE_SET_METHOD(target, "album_type", Album_Type);
NODE_SET_METHOD(target, "album_artist", Album_Artist);
NODE_SET_METHOD(target, "album_cover", Album_Cover);
target->Set(v8::String::NewSymbol("SP_IMAGE_SIZE_SMALL"), v8::Int32::New(static_cast<int>(SP_IMAGE_SIZE_SMALL)), ReadOnly);
target->Set(v8::String::NewSymbol("SP_IMAGE_SIZE_NORMAL"), v8::Int32::New(static_cast<int>(SP_IMAGE_SIZE_NORMAL)), ReadOnly);
target->Set(v8::String::NewSymbol("SP_IMAGE_SIZE_LARGE"), v8::Int32::New(static_cast<int>(SP_IMAGE_SIZE_LARGE)), ReadOnly);
}