Format: application/x-python-code
1 o
2
3 ��h�_ � @ s� d Z ddlmZ ddlmZmZmZmZm Z m
4 Z
5 ddlmZm
6 Z
7 mZ ddlmZmZ ddlmZmZmZ g d�ZG dd � d e�ZG d
8 d� de�ZG dd
9 � d
10 e�ZG dd� de�ZG dd� de�ZG dd� de�ZG dd� de�ZG dd� de�ZdS )zMVarious display related classes.
11
12 Authors : MinRK, gregcaporaso, dannystaple
13 � )�escape)�exists�isfile�splitext�abspath�join�isdir)�walk�sep�fsdecode)�
14 DisplayObject�TextDisplayObject)�Tuple�Iterable�Optional)�Audio�IFrame�YouTubeVideo�
15 VimeoVideo�ScribdDocument�FileLink� FileLinks�Codec s� e Zd ZdZdZddd�� fdd�Z� fd d
16 �Zedd� �Zed
17 e e
18 ef fdd��Zedd� �Z
19 edd� �Zdd� Zdd� Zdd� Zdd� Zdd� Z� ZS )r a� Create an audio object.
20
21 When this object is returned by an input cell or passed to the
22 display function, it will result in Audio controls being displayed
23 in the frontend (only works in the notebook).
24
25 Parameters
26 ----------
27 data : numpy array, list, unicode, str or bytes
28 Can be one of
29
30 * Numpy 1d array containing the desired waveform (mono)
31 * Numpy 2d array containing waveforms for each channel.
32 Shape=(NCHAN, NSAMPLES). For the standard channel order, see
33 http://msdn.microsoft.com/en-us/library/windows/hardware/dn653308(v=vs.85).aspx
34 * List of float or integer representing the waveform (mono)
35 * String containing the filename
36 * Bytestring containing raw PCM data or
37 * URL pointing to a file on the web.
38
39 If the array option is used, the waveform will be normalized.
40
41 If a filename or url is used, the format support will be browser
42 dependent.
43 url : unicode
44 A URL to download the data from.
45 filename : unicode
46 Path to a local file to load the data from.
47 embed : boolean
48 Should the audio data be embedded using a data URI (True) or should
49 the original source be referenced. Set this to True if you want the
50 audio to playable later with no internet connection in the notebook.
51
52 Default is `True`, unless the keyword argument `url` is set, then
53 default value is `False`.
54 rate : integer
55 The sampling rate of the raw data.
56 Only required when data parameter is being used as an array
57 autoplay : bool
58 Set to True if the audio should immediately start playing.
59 Default is `False`.
60 normalize : bool
61 Whether audio should be normalized (rescaled) to the maximum possible
62 range. Default is `True`. When set to `False`, `data` must be between
63 -1 and 1 (inclusive), otherwise an error is raised.
64 Applies only when `data` is a list or array of samples; other types of
65 audio are never normalized.
66
67 Examples
68 --------
69
70 >>> import pytest
71 >>> np = pytest.importorskip("numpy")
72
73 Generate a sound
74
75 >>> import numpy as np
76 >>> framerate = 44100
77 >>> t = np.linspace(0,5,framerate*5)
78 >>> data = np.sin(2*np.pi*220*t) + np.sin(2*np.pi*224*t)
79 >>> Audio(data, rate=framerate)
80 <IPython.lib.display.Audio object>
81
82 Can also do stereo or more channels
83
84 >>> dataleft = np.sin(2*np.pi*220*t)
85 >>> dataright = np.sin(2*np.pi*224*t)
86 >>> Audio([dataleft, dataright], rate=framerate)
87 <IPython.lib.display.Audio object>
88
89 From URL:
90
91 >>> Audio("http://www.nch.com.au/acm/8k16bitpcm.wav") # doctest: +SKIP
92 >>> Audio(url="http://www.w3schools.com/html/horse.ogg") # doctest: +SKIP
93
94 From a File:
95
96 >>> Audio('IPython/lib/tests/test.wav') # doctest: +SKIP
97 >>> Audio(filename='IPython/lib/tests/test.wav') # doctest: +SKIP
98
99 From Bytes:
100
101 >>> Audio(b'RAW_WAV_DATA..') # doctest: +SKIP
102 >>> Audio(data=b'RAW_WAV_DATA..') # doctest: +SKIP
103
104 See Also
105 --------
106 ipywidgets.Audio
107
108 Audio widget with more more flexibility and options.
109
110 �rbNFT��
111 element_idc s� |d u r|d u r|d u rt d��|du r|d u rt d��|d ur(|dur(d| _nd| _|| _|| _tt| �j|||d� | jd urYt| jt �s[|d u rOt d��t�
112 |||�| _d S d S d S )Nz6No audio data found. Expecting filename, url, or data.Fz,No url found. Expecting url when embed=FalseT��data�url�filenamezKrate must be specified when data is a numpy array or list of audio samples.)�
113 ValueError�embed�autoplayr �superr �__init__r �
114 isinstance�bytes� _make_wav) �selfr r r r! �rater"