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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use super::image::Image;
use crate::data::TypedData;
use crate::data::*;
use crate::errors::{Error, NativeError};
use crate::window::get_window;
use futures::future::{self, FutureExt, TryFutureExt};
use futures::Future;
use js_sys::{Array, ArrayBuffer, Promise, Uint8Array};
use log::info;
use std::marker::PhantomData;
use wasm_bindgen::JsCast;
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::futures_0_3::JsFuture;
use web_sys::{
    AudioBuffer, AudioContext, Blob, BlobPropertyBag, HtmlImageElement, Request, Response, Url,
};

pub fn image(url: &str) -> impl Future<Output = Result<HtmlImageElement, Error>> {
    Image::new(url)
}

pub fn image_blob(blob: &Blob) -> impl Future<Output = Result<HtmlImageElement, Error>> {
    match Url::create_object_url_with_blob(&blob) {
        Ok(url) => future::ok(url),
        Err(err) => future::err(err.into()),
    }
    .and_then(|url| image(&url))
}

pub fn image_u8<T: AsRef<[u8]>>(
    data: T,
    mime_type: &str,
) -> impl Future<Output = Result<HtmlImageElement, Error>> {
    let mut blob_opts = BlobPropertyBag::new();
    blob_opts.type_(mime_type);

    match Blob::new_with_buffer_source_sequence_and_options(
        &Array::of1(&TypedData::new(data.as_ref()).into()).into(),
        &blob_opts,
    ) {
        Ok(blob) => future::ok(blob),
        Err(err) => future::err(err.into()),
    }
    .and_then(|blob| image_blob(&blob))
}

pub fn image_u16<T: AsRef<[u16]>>(
    data: T,
    mime_type: &str,
) -> impl Future<Output = Result<HtmlImageElement, Error>> {
    let mut blob_opts = BlobPropertyBag::new();
    blob_opts.type_(mime_type);

    match Blob::new_with_buffer_source_sequence_and_options(
        &Array::of1(&TypedData::new(data.as_ref()).into()).into(),
        &blob_opts,
    ) {
        Ok(blob) => future::ok(blob),
        Err(err) => future::err(err.into()),
    }
    .and_then(|blob| image_blob(&blob))
}

pub fn image_u32<T: AsRef<[u32]>>(
    data: T,
    mime_type: &str,
) -> impl Future<Output = Result<HtmlImageElement, Error>> {
    let mut blob_opts = BlobPropertyBag::new();
    blob_opts.type_(mime_type);

    match Blob::new_with_buffer_source_sequence_and_options(
        &Array::of1(&TypedData::new(data.as_ref()).into()).into(),
        &blob_opts,
    ) {
        Ok(blob) => future::ok(blob),
        Err(err) => future::err(err.into()),
    }
    .and_then(|blob| image_blob(&blob))
}

pub fn image_i8<T: AsRef<[i8]>>(
    data: T,
    mime_type: &str,
) -> impl Future<Output = Result<HtmlImageElement, Error>> {
    let mut blob_opts = BlobPropertyBag::new();
    blob_opts.type_(mime_type);

    match Blob::new_with_buffer_source_sequence_and_options(
        &Array::of1(&TypedData::new(data.as_ref()).into()).into(),
        &blob_opts,
    ) {
        Ok(blob) => future::ok(blob),
        Err(err) => future::err(err.into()),
    }
    .and_then(|blob| image_blob(&blob))
}

pub fn image_i16<T: AsRef<[i16]>>(
    data: T,
    mime_type: &str,
) -> impl Future<Output = Result<HtmlImageElement, Error>> {
    let mut blob_opts = BlobPropertyBag::new();
    blob_opts.type_(mime_type);

    match Blob::new_with_buffer_source_sequence_and_options(
        &Array::of1(&TypedData::new(data.as_ref()).into()).into(),
        &blob_opts,
    ) {
        Ok(blob) => future::ok(blob),
        Err(err) => future::err(err.into()),
    }
    .and_then(|blob| image_blob(&blob))
}

pub fn image_i32<T: AsRef<[i32]>>(
    data: T,
    mime_type: &str,
) -> impl Future<Output = Result<HtmlImageElement, Error>> {
    let mut blob_opts = BlobPropertyBag::new();
    blob_opts.type_(mime_type);

    match Blob::new_with_buffer_source_sequence_and_options(
        &Array::of1(&TypedData::new(data.as_ref()).into()).into(),
        &blob_opts,
    ) {
        Ok(blob) => future::ok(blob),
        Err(err) => future::err(err.into()),
    }
    .and_then(|blob| image_blob(&blob))
}

pub fn image_f32<T: AsRef<[f32]>>(
    data: T,
    mime_type: &str,
) -> impl Future<Output = Result<HtmlImageElement, Error>> {
    let mut blob_opts = BlobPropertyBag::new();
    blob_opts.type_(mime_type);

    match Blob::new_with_buffer_source_sequence_and_options(
        &Array::of1(&TypedData::new(data.as_ref()).into()).into(),
        &blob_opts,
    ) {
        Ok(blob) => future::ok(blob),
        Err(err) => future::err(err.into()),
    }
    .and_then(|blob| image_blob(&blob))
}

pub fn image_f64<T: AsRef<[f64]>>(
    data: T,
    mime_type: &str,
) -> impl Future<Output = Result<HtmlImageElement, Error>> {
    let mut blob_opts = BlobPropertyBag::new();
    blob_opts.type_(mime_type);

    match Blob::new_with_buffer_source_sequence_and_options(
        &Array::of1(&TypedData::new(data.as_ref()).into()).into(),
        &blob_opts,
    ) {
        Ok(blob) => future::ok(blob),
        Err(err) => future::err(err.into()),
    }
    .and_then(|blob| image_blob(&blob))
}

//Audio
pub fn audio<'a>(
    url: &str,
    ctx: &'a AudioContext,
) -> impl Future<Output = Result<AudioBuffer, Error>> + 'a {
    array_buffer(&url).and_then(move |buf| audio_buffer(&buf, &ctx))
}

pub fn audio_buffer<'a>(
    array_buffer: &ArrayBuffer,
    ctx: &AudioContext,
) -> impl Future<Output = Result<AudioBuffer, Error>> {
    match ctx.decode_audio_data(&array_buffer) {
        Ok(promise) => future::ok(promise),
        Err(err) => future::err(err.into()),
    }
    .and_then(|promise| JsFuture::from(promise))
    .map(|res| match res {
        Ok(x) => Ok(AudioBuffer::from(x)),
        Err(x) => Err(Error::from(x)),
    })
}

//convenince helpers for loading slices, vecs, etc.
pub fn audio_u8<T: AsRef<[u8]>>(
    data: T,
    ctx: &AudioContext,
) -> impl Future<Output = Result<AudioBuffer, Error>> {
    let array_buffer: ArrayBuffer = TypedData::new(data.as_ref()).into();
    audio_buffer(&array_buffer, &ctx)
}

pub fn audio_u16<T: AsRef<[u16]>>(
    data: T,
    ctx: &AudioContext,
) -> impl Future<Output = Result<AudioBuffer, Error>> {
    let array_buffer: ArrayBuffer = TypedData::new(data.as_ref()).into();
    audio_buffer(&array_buffer, &ctx)
}
pub fn audio_u32<T: AsRef<[u32]>>(
    data: T,
    ctx: &AudioContext,
) -> impl Future<Output = Result<AudioBuffer, Error>> {
    let array_buffer: ArrayBuffer = TypedData::new(data.as_ref()).into();
    audio_buffer(&array_buffer, &ctx)
}
pub fn audio_i8<T: AsRef<[i8]>>(
    data: T,
    ctx: &AudioContext,
) -> impl Future<Output = Result<AudioBuffer, Error>> {
    let array_buffer: ArrayBuffer = TypedData::new(data.as_ref()).into();
    audio_buffer(&array_buffer, &ctx)
}
pub fn audio_i16<T: AsRef<[i16]>>(
    data: T,
    ctx: &AudioContext,
) -> impl Future<Output = Result<AudioBuffer, Error>> {
    let array_buffer: ArrayBuffer = TypedData::new(data.as_ref()).into();
    audio_buffer(&array_buffer, &ctx)
}
pub fn audio_i32<T: AsRef<[i32]>>(
    data: T,
    ctx: &AudioContext,
) -> impl Future<Output = Result<AudioBuffer, Error>> {
    let array_buffer: ArrayBuffer = TypedData::new(data.as_ref()).into();
    audio_buffer(&array_buffer, &ctx)
}
pub fn audio_f32<T: AsRef<[f32]>>(
    data: T,
    ctx: &AudioContext,
) -> impl Future<Output = Result<AudioBuffer, Error>> {
    let array_buffer: ArrayBuffer = TypedData::new(data.as_ref()).into();
    audio_buffer(&array_buffer, &ctx)
}
pub fn audio_f64<T: AsRef<[f64]>>(
    data: T,
    ctx: &AudioContext,
) -> impl Future<Output = Result<AudioBuffer, Error>> {
    let array_buffer: ArrayBuffer = TypedData::new(data.as_ref()).into();
    audio_buffer(&array_buffer, &ctx)
}

//text
pub fn text(url: &str) -> impl Future<Output = Result<String, Error>> {
    let req = Request::new_with_str(url);

    async {
        let req = req?;

        let resp: Response = request(&req).await?;

        let promise = resp.text()?;

        let data = JsFuture::from(promise).await?;

        let text = data.as_string().ok_or(Error::from(NativeError::Internal))?;

        Ok(text)
    }
}

pub fn array_buffer(url: &str) -> impl Future<Output = Result<ArrayBuffer, Error>> {
    let req = Request::new_with_str(url);

    async {
        let req = req?;

        let resp: Response = request(&req).await?;

        let promise = resp.array_buffer()?;

        let data = JsFuture::from(promise).await?;

        Ok(data.into())
    }
}

pub fn vec_f32(url: &str) -> impl Future<Output = Result<Vec<f32>, Error>> {
    let url = url.to_owned();
    async move {
        let data = array_buffer(&url).await?;
        let data: ArrayBuffer = data.into();
        Ok(clone_to_vec_f32(&js_sys::Float32Array::new(&data)))
    }
}

pub fn vec_f64(url: &str) -> impl Future<Output = Result<Vec<f64>, Error>> {
    let url = url.to_owned();
    async move {
        let data = array_buffer(&url).await?;
        let data: ArrayBuffer = data.into();
        Ok(clone_to_vec_f64(&js_sys::Float64Array::new(&data)))
    }
}

pub fn vec_i8(url: &str) -> impl Future<Output = Result<Vec<i8>, Error>> {
    let url = url.to_owned();
    async move {
        let data = array_buffer(&url).await?;
        let data: ArrayBuffer = data.into();
        Ok(clone_to_vec_i8(&js_sys::Int8Array::new(&data)))
    }
}
pub fn vec_i16(url: &str) -> impl Future<Output = Result<Vec<i16>, Error>> {
    let url = url.to_owned();
    async move {
        let data = array_buffer(&url).await?;
        let data: ArrayBuffer = data.into();
        Ok(clone_to_vec_i16(&js_sys::Int16Array::new(&data)))
    }
}
pub fn vec_i32(url: &str) -> impl Future<Output = Result<Vec<i32>, Error>> {
    let url = url.to_owned();
    async move {
        let data = array_buffer(&url).await?;
        let data: ArrayBuffer = data.into();
        Ok(clone_to_vec_i32(&js_sys::Int32Array::new(&data)))
    }
}

pub fn vec_u8(url: &str) -> impl Future<Output = Result<Vec<u8>, Error>> {
    let url = url.to_owned();
    async move {
        let data = array_buffer(&url).await?;
        let data: ArrayBuffer = data.into();
        Ok(clone_to_vec_u8(&js_sys::Uint8Array::new(&data)))
    }
}
pub fn vec_u16(url: &str) -> impl Future<Output = Result<Vec<u16>, Error>> {
    let url = url.to_owned();
    async move {
        let data = array_buffer(&url).await?;
        let data: ArrayBuffer = data.into();
        Ok(clone_to_vec_u16(&js_sys::Uint16Array::new(&data)))
    }
}
pub fn vec_u32(url: &str) -> impl Future<Output = Result<Vec<u32>, Error>> {
    let url = url.to_owned();
    async move {
        let data = array_buffer(&url).await?;
        let data: ArrayBuffer = data.into();
        Ok(clone_to_vec_u32(&js_sys::Uint32Array::new(&data)))
    }
}

pub fn request(req: &Request) -> impl Future<Output = Result<Response, Error>> {
    let promise: Result<Promise, Error> =
        get_window().map(|window| window.fetch_with_request(&req));

    async {
        let promise = promise?;

        let resp_value = JsFuture::from(promise).await?;
        let resp: Response = resp_value.dyn_into()?;

        Ok(resp)
    }
}