io.html 253 KB
Newer Older
Anthony Larcher's avatar
Anthony Larcher committed
1

Anthony Larcher's avatar
Anthony Larcher committed
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Anthony Larcher's avatar
Anthony Larcher committed
4

Anthony Larcher's avatar
Anthony Larcher committed
5
6
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
Anthony Larcher's avatar
Anthony Larcher committed
7
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
Anthony Larcher's avatar
Anthony Larcher committed
8
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Anthony Larcher's avatar
doc    
Anthony Larcher committed
9
    <title>frontend.io &#8212; SIDEKIT  documentation</title>
Anthony Larcher's avatar
Anthony Larcher committed
10
    <link rel="stylesheet" href="../../_static/sphinxdoc.css" type="text/css" />
Anthony Larcher's avatar
Anthony Larcher committed
11
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
Anthony Larcher's avatar
Anthony Larcher committed
12
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
Anthony Larcher's avatar
Anthony Larcher committed
13
14
15
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
16
    <link rel="index" title="Index" href="../../genindex.html" />
Anthony Larcher's avatar
Anthony Larcher committed
17
    <link rel="search" title="Search" href="../../search.html" /> 
Anthony Larcher's avatar
Anthony Larcher committed
18
  </head><body>
Anthony Larcher's avatar
Anthony Larcher committed
19
20
21
22
23
24
25
26
27
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
28
29
        <li class="nav-item nav-item-0"><a href="../../index.html">SIDEKIT  documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Module code</a> &#187;</li> 
Anthony Larcher's avatar
Anthony Larcher committed
30
      </ul>
Anthony Larcher's avatar
Anthony Larcher committed
31
32
33
34
35
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
Anthony Larcher's avatar
Anthony Larcher committed
36
    <div class="searchformwrapper">
Anthony Larcher's avatar
Anthony Larcher committed
37
    <form class="search" action="../../search.html" method="get">
Anthony Larcher's avatar
Anthony Larcher committed
38
39
      <input type="text" name="q" />
      <input type="submit" value="Go" />
Anthony Larcher's avatar
Anthony Larcher committed
40
41
42
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
Anthony Larcher's avatar
Anthony Larcher committed
43
    </div>
Anthony Larcher's avatar
Anthony Larcher committed
44
45
46
47
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
Anthony Larcher's avatar
Anthony Larcher committed
48
49
50
51
52

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
Anthony Larcher's avatar
Anthony Larcher committed
53
54
            
  <h1>Source code for frontend.io</h1><div class="highlight"><pre>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="c1">#</span>
<span class="c1"># This file is part of SIDEKIT.</span>
<span class="c1">#</span>
<span class="c1"># SIDEKIT is a python package for speaker verification.</span>
<span class="c1"># Home page: http://www-lium.univ-lemans.fr/sidekit/</span>
<span class="c1">#</span>
<span class="c1"># SIDEKIT is a python package for speaker verification.</span>
<span class="c1"># Home page: http://www-lium.univ-lemans.fr/sidekit/</span>
<span class="c1">#    </span>
<span class="c1"># SIDEKIT is free software: you can redistribute it and/or modify</span>
<span class="c1"># it under the terms of the GNU LLesser General Public License as </span>
<span class="c1"># published by the Free Software Foundation, either version 3 of the License, </span>
<span class="c1"># or (at your option) any later version.</span>
<span class="c1">#</span>
<span class="c1"># SIDEKIT is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c1"># GNU Lesser General Public License for more details.</span>
<span class="c1">#</span>
<span class="c1"># You should have received a copy of the GNU Lesser General Public License</span>
<span class="c1"># along with SIDEKIT.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
Anthony Larcher's avatar
Anthony Larcher committed
77
78

<span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
79
<span class="sd">Copyright 2014-2018 Anthony Larcher</span>
Anthony Larcher's avatar
Anthony Larcher committed
80
81
82
83

<span class="sd">:mod:`frontend` provides methods to process an audio signal in order to extract</span>
<span class="sd">useful parameters for speaker verification.</span>
<span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
84
85
86
87
<span class="kn">import</span> <span class="nn">audioop</span>
<span class="kn">import</span> <span class="nn">decimal</span>
<span class="kn">import</span> <span class="nn">h5py</span>
<span class="kn">import</span> <span class="nn">logging</span>
Anthony Larcher's avatar
Anthony Larcher committed
88
<span class="kn">import</span> <span class="nn">math</span>
Anthony Larcher's avatar
Anthony Larcher committed
89
<span class="kn">import</span> <span class="nn">numpy</span>
Anthony Larcher's avatar
Anthony Larcher committed
90
<span class="kn">import</span> <span class="nn">os</span>
Anthony Larcher's avatar
Anthony Larcher committed
91
92
<span class="kn">import</span> <span class="nn">struct</span>
<span class="kn">import</span> <span class="nn">warnings</span>
Anthony Larcher's avatar
Anthony Larcher committed
93
<span class="kn">import</span> <span class="nn">wave</span>
Anthony Larcher's avatar
Anthony Larcher committed
94
<span class="kn">import</span> <span class="nn">scipy.signal</span>
Anthony Larcher's avatar
Anthony Larcher committed
95

Anthony Larcher's avatar
doc    
Anthony Larcher committed
96
97
<span class="kn">from</span> <span class="nn">scipy.signal</span> <span class="k">import</span> <span class="n">decimate</span>
<span class="kn">from</span> <span class="nn">sidekit.sidekit_wrappers</span> <span class="k">import</span> <span class="n">check_path_existance</span>
Anthony Larcher's avatar
Anthony Larcher committed
98

Anthony Larcher's avatar
Anthony Larcher committed
99

Anthony Larcher's avatar
doc    
Anthony Larcher committed
100
<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Anthony Larcher&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
101
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2014-2018 Anthony Larcher&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
102
103
104
105
106
<span class="n">__license__</span> <span class="o">=</span> <span class="s2">&quot;LGPL&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Anthony Larcher&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;anthony.larcher@univ-lemans.fr&quot;</span>
<span class="n">__status__</span> <span class="o">=</span> <span class="s2">&quot;Production&quot;</span>
<span class="n">__docformat__</span> <span class="o">=</span> <span class="s1">&#39;reStructuredText&#39;</span>
Anthony Larcher's avatar
Anthony Larcher committed
107
108


Anthony Larcher's avatar
doc    
Anthony Larcher committed
109
<span class="c1"># HTK parameters</span>
Anthony Larcher's avatar
Anthony Larcher committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<span class="n">WAVEFORM</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">LPC</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">LPCREFC</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">LPCEPSTRA</span> <span class="o">=</span> <span class="mi">3</span>
<span class="n">LPCDELCEP</span> <span class="o">=</span> <span class="mi">4</span>
<span class="n">IREFC</span> <span class="o">=</span> <span class="mi">5</span>
<span class="n">MFCC</span> <span class="o">=</span> <span class="mi">6</span>
<span class="n">FBANK</span> <span class="o">=</span> <span class="mi">7</span>
<span class="n">MELSPEC</span> <span class="o">=</span> <span class="mi">8</span>
<span class="n">USER</span> <span class="o">=</span> <span class="mi">9</span>
<span class="n">DISCRETE</span> <span class="o">=</span> <span class="mi">10</span>
<span class="n">PLP</span> <span class="o">=</span> <span class="mi">11</span>
<span class="n">ANON</span> <span class="o">=</span> <span class="mi">12</span>

Anthony Larcher's avatar
doc    
Anthony Larcher committed
124
125
126
127
128
129
130
131
132
133
<span class="n">_E</span> <span class="o">=</span> <span class="mo">0o000100</span>  <span class="c1"># has energy</span>
<span class="n">_N</span> <span class="o">=</span> <span class="mo">0o000200</span>  <span class="c1"># absolute energy supressed</span>
<span class="n">_D</span> <span class="o">=</span> <span class="mo">0o000400</span>  <span class="c1"># has delta coefficients</span>
<span class="n">_A</span> <span class="o">=</span> <span class="mo">0o001000</span>  <span class="c1"># has acceleration coefficients</span>
<span class="n">_C</span> <span class="o">=</span> <span class="mo">0o002000</span>  <span class="c1"># is compressed</span>
<span class="n">_Z</span> <span class="o">=</span> <span class="mo">0o004000</span>  <span class="c1"># has zero mean static coef.</span>
<span class="n">_K</span> <span class="o">=</span> <span class="mo">0o010000</span>  <span class="c1"># has CRC checksum</span>
<span class="n">_0</span> <span class="o">=</span> <span class="mo">0o020000</span>  <span class="c1"># has 0th cepstral coef.</span>
<span class="n">_V</span> <span class="o">=</span> <span class="mo">0o040000</span>  <span class="c1"># has VQ data</span>
<span class="n">_T</span> <span class="o">=</span> <span class="mo">0o100000</span>  <span class="c1"># has third differential coef.</span>
Anthony Larcher's avatar
Anthony Larcher committed
134
135
136
137

<span class="n">parms16bit</span> <span class="o">=</span> <span class="p">[</span><span class="n">WAVEFORM</span><span class="p">,</span> <span class="n">IREFC</span><span class="p">,</span> <span class="n">DISCRETE</span><span class="p">]</span>


Anthony Larcher's avatar
update    
Anthony Larcher committed
138
<span class="nd">@check_path_existance</span>
Anthony Larcher's avatar
Anthony Larcher committed
139
<span class="k">def</span> <span class="nf">write_pcm</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">output_file_name</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
140
141
142
    <span class="sd">&quot;&quot;&quot;Write signal to single channel PCM 16 bits</span>
<span class="sd">    </span>
<span class="sd">    :param data: audio signal to write in a RAW PCM file.</span>
Anthony Larcher's avatar
Anthony Larcher committed
143
<span class="sd">    :param output_file_name: name of the file to write</span>
Anthony Larcher's avatar
Anthony Larcher committed
144
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
145
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">output_file_name</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">of</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
146
        <span class="k">if</span> <span class="n">numpy</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">data</span><span class="p">)</span><span class="o">.</span><span class="n">max</span><span class="p">()</span> <span class="o">&lt;</span> <span class="mf">1.</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
147
148
            <span class="n">data</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">*</span> <span class="mi">16384</span><span class="p">,</span> <span class="n">decimals</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&#39;int16&#39;</span><span class="p">)</span>
        <span class="n">of</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;&#39;</span> <span class="o">+</span> <span class="s1">&#39;h&#39;</span> <span class="o">*</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="o">*</span><span class="n">data</span><span class="p">))</span>
Anthony Larcher's avatar
Anthony Larcher committed
149

Anthony Larcher's avatar
update    
Anthony Larcher committed
150

Anthony Larcher's avatar
Anthony Larcher committed
151
<div class="viewcode-block" id="read_pcm"><a class="viewcode-back" href="../../api/frontend/io.html#frontend.io.read_pcm">[docs]</a><span class="k">def</span> <span class="nf">read_pcm</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
152
153
    <span class="sd">&quot;&quot;&quot;Read signal from single channel PCM 16 bits</span>

Anthony Larcher's avatar
Anthony Larcher committed
154
<span class="sd">    :param input_file_name: name of the PCM file to read.</span>
Anthony Larcher's avatar
Anthony Larcher committed
155
<span class="sd">    </span>
Anthony Larcher's avatar
Anthony Larcher committed
156
<span class="sd">    :return: the audio signal read from the file in a ndarray encoded  on 16 bits, None and 2 (depth of the encoding in bytes)</span>
Anthony Larcher's avatar
Anthony Larcher committed
157
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
158
159
160
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="n">f</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>  <span class="c1"># Go to te end of the file</span>
        <span class="c1"># get the sample count</span>
Anthony Larcher's avatar
Anthony Larcher committed
161
        <span class="n">sample_count</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">tell</span><span class="p">()</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
162
163
164
        <span class="n">f</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>  <span class="c1"># got to the begining of the file</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;&#39;</span> <span class="o">+</span> <span class="s1">&#39;h&#39;</span> <span class="o">*</span> <span class="n">sample_count</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()))</span>
    <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">float32</span><span class="p">),</span> <span class="kc">None</span><span class="p">,</span> <span class="mi">2</span></div>
Anthony Larcher's avatar
Anthony Larcher committed
165

Anthony Larcher's avatar
Anthony Larcher committed
166

Anthony Larcher's avatar
Anthony Larcher committed
167
<div class="viewcode-block" id="read_wav"><a class="viewcode-back" href="../../api/frontend/io.html#frontend.io.read_wav">[docs]</a><span class="k">def</span> <span class="nf">read_wav</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
168
169
170
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    :param input_file_name:</span>
<span class="sd">    :return:</span>
Anthony Larcher's avatar
Anthony Larcher committed
171
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
172
    <span class="k">with</span> <span class="n">wave</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">,</span> <span class="s2">&quot;r&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">wfh</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
173
174
        <span class="p">(</span><span class="n">nchannels</span><span class="p">,</span> <span class="n">sampwidth</span><span class="p">,</span> <span class="n">framerate</span><span class="p">,</span> <span class="n">nframes</span><span class="p">,</span> <span class="n">comptype</span><span class="p">,</span> <span class="n">compname</span><span class="p">)</span> <span class="o">=</span> <span class="n">wfh</span><span class="o">.</span><span class="n">getparams</span><span class="p">()</span>
        <span class="n">raw</span> <span class="o">=</span> <span class="n">wfh</span><span class="o">.</span><span class="n">readframes</span><span class="p">(</span><span class="n">nframes</span> <span class="o">*</span> <span class="n">nchannels</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
175
        <span class="n">out</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack_from</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2">h&quot;</span> <span class="o">%</span> <span class="n">nframes</span> <span class="o">*</span> <span class="n">nchannels</span><span class="p">,</span> <span class="n">raw</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
176
        <span class="n">sig</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">out</span><span class="p">),</span> <span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">nchannels</span><span class="p">))</span><span class="o">.</span><span class="n">squeeze</span><span class="p">()</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
177
        <span class="k">return</span> <span class="n">sig</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">float32</span><span class="p">),</span> <span class="n">framerate</span><span class="p">,</span> <span class="n">sampwidth</span></div>
Anthony Larcher's avatar
Anthony Larcher committed
178
    
Anthony Larcher's avatar
doc    
Anthony Larcher committed
179

Anthony Larcher's avatar
Anthony Larcher committed
180
<div class="viewcode-block" id="pcmu2lin"><a class="viewcode-back" href="../../api/frontend/io.html#frontend.io.pcmu2lin">[docs]</a><span class="k">def</span> <span class="nf">pcmu2lin</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">s</span><span class="o">=</span><span class="mf">4004.189931</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    <span class="sd">&quot;&quot;&quot;Convert Mu-law PCM to linear X=(P,S)</span>
<span class="sd">    lin = pcmu2lin(pcmu) where pcmu contains a vector</span>
<span class="sd">    of mu-law values in the range 0 to 255.</span>
<span class="sd">    No checking is performed to see that numbers are in this range.</span>

<span class="sd">    Output values are divided by the scale factor s:</span>

<span class="sd">        s		Output Range</span>
<span class="sd">        1		+-8031	(integer values)</span>
<span class="sd">        4004.2	+-2.005649 (default)</span>
<span class="sd">        8031		+-1</span>
<span class="sd">        8159		+-0.9843118 (+-1 nominal full scale)</span>

<span class="sd">    The default scaling factor 4004.189931 is equal to</span>
<span class="sd">    sqrt((2207^2 + 5215^2)/2) this follows ITU standard G.711.</span>
<span class="sd">    The sine wave with PCM-Mu values [158 139 139 158 30 11 11 30]</span>
<span class="sd">    has a mean square value of unity corresponding to 0 dBm0.</span>
Anthony Larcher's avatar
Anthony Larcher committed
198
199
<span class="sd">    :param p: input signal encoded in PCM mu-law to convert</span>
<span class="sd">    :param s: conversion value from mu-scale oto linear scale</span>
Anthony Larcher's avatar
Anthony Larcher committed
200
201
202
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">t</span> <span class="o">=</span> <span class="mi">4</span> <span class="o">/</span> <span class="n">s</span>
    <span class="n">m</span> <span class="o">=</span> <span class="mi">15</span> <span class="o">-</span> <span class="p">(</span><span class="n">p</span> <span class="o">%</span> <span class="mi">16</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
203
    <span class="n">q</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">p</span> <span class="o">//</span> <span class="mi">128</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
204
    <span class="n">e</span> <span class="o">=</span> <span class="p">(</span><span class="mi">127</span> <span class="o">-</span> <span class="n">p</span> <span class="o">-</span> <span class="n">m</span> <span class="o">+</span> <span class="mi">128</span> <span class="o">*</span> <span class="n">q</span><span class="p">)</span> <span class="o">/</span> <span class="mi">16</span>
Anthony Larcher's avatar
Anthony Larcher committed
205
    <span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="n">m</span> <span class="o">+</span> <span class="mf">16.5</span><span class="p">)</span> <span class="o">*</span> <span class="n">numpy</span><span class="o">.</span><span class="n">power</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span> <span class="o">-</span> <span class="mf">16.5</span>
Anthony Larcher's avatar
Anthony Larcher committed
206
    <span class="n">z</span> <span class="o">=</span> <span class="p">(</span><span class="n">q</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span> <span class="o">*</span> <span class="n">x</span> <span class="o">*</span> <span class="n">t</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
207
    <span class="k">return</span> <span class="n">z</span></div>
Anthony Larcher's avatar
Anthony Larcher committed
208

Anthony Larcher's avatar
doc    
Anthony Larcher committed
209

Anthony Larcher's avatar
Anthony Larcher committed
210
<div class="viewcode-block" id="read_sph"><a class="viewcode-back" href="../../api/frontend/io.html#frontend.io.read_sph">[docs]</a><span class="k">def</span> <span class="nf">read_sph</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;p&#39;</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
211
212
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Read a SPHERE audio file</span>
Anthony Larcher's avatar
Anthony Larcher committed
213

Anthony Larcher's avatar
Anthony Larcher committed
214
<span class="sd">    :param input_file_name: name of the file to read</span>
Anthony Larcher's avatar
Anthony Larcher committed
215
216
217
218
219
220
221
222
223
<span class="sd">    :param mode: specifies the following (\* =default)</span>
<span class="sd">    </span>
<span class="sd">    .. note::</span>
<span class="sd">    </span>
<span class="sd">        - Scaling:</span>
<span class="sd">        </span>
<span class="sd">            - &#39;s&#39;    Auto scale to make data peak = +-1 (use with caution if reading in chunks)</span>
<span class="sd">            - &#39;r&#39;    Raw unscaled data (integer values)</span>
<span class="sd">            - &#39;p&#39;    Scaled to make +-1 equal full scale</span>
Anthony Larcher's avatar
Anthony Larcher committed
224
225
226
227
<span class="sd">            - &#39;o&#39;    Scale to bin centre rather than bin edge (e.g. 127 rather than 127.5 for 8 bit values,</span>
<span class="sd">                     can be combined with n+p,r,s modes)</span>
<span class="sd">            - &#39;n&#39;    Scale to negative peak rather than positive peak (e.g. 128.5 rather than 127.5 for 8 bit values,</span>
<span class="sd">                     can be combined with o+p,r,s modes)</span>
Anthony Larcher's avatar
Anthony Larcher committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241

<span class="sd">        - Format</span>
<span class="sd">       </span>
<span class="sd">           - &#39;l&#39;    Little endian data (Intel,DEC) (overrides indication in file)</span>
<span class="sd">           - &#39;b&#39;    Big endian data (non Intel/DEC) (overrides indication in file)</span>

<span class="sd">       - File I/O</span>
<span class="sd">       </span>
<span class="sd">           - &#39;f&#39;    Do not close file on exit</span>
<span class="sd">           - &#39;d&#39;    Look in data directory: voicebox(&#39;dir_data&#39;)</span>
<span class="sd">           - &#39;w&#39;    Also read the annotation file \*.wrd if present (as in TIMIT)</span>
<span class="sd">           - &#39;t&#39;    Also read the phonetic transcription file \*.phn if present (as in TIMIT)</span>

<span class="sd">        - NMAX     maximum number of samples to read (or -1 for unlimited [default])</span>
Anthony Larcher's avatar
Anthony Larcher committed
242
243
<span class="sd">        - NSKIP    number of samples to skip from start of file (or -1 to continue from previous read when FFX</span>
<span class="sd">                   is given instead of FILENAME [default])</span>
Anthony Larcher's avatar
Anthony Larcher committed
244
245
246
247
248
249
250

<span class="sd">    :return: a tupple such that (Y, FS)</span>
<span class="sd">    </span>
<span class="sd">    .. note::</span>
<span class="sd">    </span>
<span class="sd">        - Y data matrix of dimension (samples,channels)</span>
<span class="sd">        - FS         sample frequency in Hz</span>
Anthony Larcher's avatar
Anthony Larcher committed
251
252
253
254
<span class="sd">        - WRD{\*,2}  cell array with word annotations: WRD{\*,:)={[t_start t_end],&#39;text&#39;} where times are in seconds</span>
<span class="sd">                     only present if &#39;w&#39; option is given</span>
<span class="sd">        - PHN{\*,2}  cell array with phoneme annotations: PHN{\*,:)={[t_start	t_end],&#39;phoneme&#39;} where times</span>
<span class="sd">                     are in seconds only present if &#39;t&#39; option is present</span>
Anthony Larcher's avatar
Anthony Larcher committed
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
<span class="sd">        - FFX        Cell array containing</span>

<span class="sd">            1. filename</span>
<span class="sd">            2. header information</span>
<span class="sd">        </span>
<span class="sd">            1. first header field name</span>
<span class="sd">            2. first header field value</span>
<span class="sd">            3. format string (e.g. NIST_1A)</span>
<span class="sd">            4. </span>
<span class="sd">                1. file id</span>
<span class="sd">                2. current position in file</span>
<span class="sd">                3. dataoff    byte offset in file to start of data</span>
<span class="sd">                4. order  byte order (l or b)</span>
<span class="sd">                5. nsamp    number of samples</span>
<span class="sd">                6. number of channels</span>
<span class="sd">                7. nbytes    bytes per data value</span>
<span class="sd">                8. bits    number of bits of precision</span>
<span class="sd">                9. fs	sample frequency</span>
<span class="sd">                10. min value</span>
<span class="sd">                11. max value</span>
<span class="sd">                12. coding 0=PCM,1=uLAW + 0=no compression, 0=shorten,20=wavpack,30=shortpack</span>
<span class="sd">                13. file not yet decompressed</span>
<span class="sd">                </span>
<span class="sd">            5. temporary filename</span>

<span class="sd">    If no output parameters are specified,</span>
<span class="sd">    header information will be printed.</span>
<span class="sd">    The code to decode shorten-encoded files, is </span>
<span class="sd">    not yet released with this toolkit.</span>
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
285
286
287
288
289
290
291
292
293
294
295
296
297
    <span class="n">codings</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">([(</span><span class="s1">&#39;pcm&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;ulaw&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)])</span>
    <span class="n">compressions</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">([(</span><span class="s1">&#39;,embedded-shorten-&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
                         <span class="p">(</span><span class="s1">&#39;,embedded-wavpack-&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
                         <span class="p">(</span><span class="s1">&#39;,embedded-shortpack-&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)])</span>
    <span class="n">byteorder</span> <span class="o">=</span> <span class="s1">&#39;l&#39;</span>
    <span class="n">endianess</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">([(</span><span class="s1">&#39;l&#39;</span><span class="p">,</span> <span class="s1">&#39;&lt;&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;&gt;&#39;</span><span class="p">)])</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">mode</span> <span class="o">==</span> <span class="s1">&#39;p&#39;</span><span class="p">:</span>
        <span class="n">mode</span> <span class="o">=</span> <span class="p">[</span><span class="n">mode</span><span class="p">,</span> <span class="s1">&#39;p&#39;</span><span class="p">]</span>
    <span class="n">k</span> <span class="o">=</span> <span class="nb">list</span><span class="p">((</span><span class="n">m</span> <span class="o">&gt;=</span> <span class="s1">&#39;p&#39;</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">m</span> <span class="o">&lt;=</span> <span class="s1">&#39;s&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">mode</span><span class="p">)</span>
    <span class="c1"># scale to input limits not output limits</span>
    <span class="n">mno</span> <span class="o">=</span> <span class="nb">all</span><span class="p">([</span><span class="n">m</span> <span class="o">!=</span> <span class="s1">&#39;o&#39;</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">mode</span><span class="p">])</span>
    <span class="n">sc</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
Anthony Larcher's avatar
Anthony Larcher committed
298
299
    <span class="k">if</span> <span class="n">k</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
        <span class="n">sc</span> <span class="o">=</span> <span class="n">mode</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
300
301
302
303
304
305
    <span class="c1"># Get byte order (little/big endian)</span>
    <span class="k">if</span> <span class="nb">any</span><span class="p">([</span><span class="n">m</span> <span class="o">==</span> <span class="s1">&#39;l&#39;</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">mode</span><span class="p">]):</span>
        <span class="n">byteorder</span> <span class="o">=</span> <span class="s1">&#39;l&#39;</span>
    <span class="k">elif</span> <span class="nb">any</span><span class="p">([</span><span class="n">m</span> <span class="o">==</span> <span class="s1">&#39;b&#39;</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">mode</span><span class="p">]):</span>
        <span class="n">byteorder</span> <span class="o">=</span> <span class="s1">&#39;b&#39;</span>
    <span class="n">ffx</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
306

Anthony Larcher's avatar
Anthony Larcher committed
307
308
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">):</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
309
310
311
312
            <span class="n">fid</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">input_file_name</span><span class="p">,</span> <span class="s1">&#39;.sph&#39;</span><span class="p">))):</span>
            <span class="n">input_file_name</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">input_file_name</span><span class="p">,</span> <span class="s1">&#39;.sph&#39;</span><span class="p">))</span>
            <span class="n">fid</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
313
        <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
314
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;Cannot find file </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">))</span>
Anthony Larcher's avatar
Anthony Larcher committed
315
316
317
        <span class="n">ffx</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">input_file_name</span>
    <span class="k">elif</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">ffx</span> <span class="o">=</span> <span class="n">input_file_name</span>
Anthony Larcher's avatar
Anthony Larcher committed
318
    <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
319
        <span class="n">fid</span> <span class="o">=</span> <span class="n">input_file_name</span>
Anthony Larcher's avatar
Anthony Larcher committed
320

Anthony Larcher's avatar
doc    
Anthony Larcher committed
321
322
323
324
325
326
327
    <span class="c1"># Read the header</span>
    <span class="k">if</span> <span class="n">ffx</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
        <span class="n">fid</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>  <span class="c1"># go to the begining of the file</span>
        <span class="n">l1</span> <span class="o">=</span> <span class="n">fid</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span>
        <span class="n">l2</span> <span class="o">=</span> <span class="n">fid</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">l1</span> <span class="o">==</span> <span class="s1">&#39;NIST_1A</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">l2</span> <span class="o">==</span> <span class="s1">&#39;   1024</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">&#39;File does not begin with a SPHERE header&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
328
329
330
        <span class="n">ffx</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">l1</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span>
        <span class="n">hlen</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">l2</span><span class="p">[</span><span class="mi">3</span><span class="p">:</span><span class="mi">7</span><span class="p">])</span>
        <span class="n">hdr</span> <span class="o">=</span> <span class="p">{}</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
331
332
333
334
335
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>  <span class="c1"># Read the header and fill a dictionary</span>
            <span class="n">st</span> <span class="o">=</span> <span class="n">fid</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">st</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="s1">&#39;;&#39;</span><span class="p">:</span>
                <span class="n">elt</span> <span class="o">=</span> <span class="n">st</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">elt</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;end_head&#39;</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
336
                    <span class="k">break</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
337
338
                <span class="k">if</span> <span class="n">elt</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="s1">&#39;-&#39;</span><span class="p">:</span>
                    <span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">&#39;Missing &#39;&#39;-&#39;&#39; in SPHERE header&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
339
                    <span class="k">break</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
340
                <span class="k">if</span> <span class="n">elt</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;s&#39;</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
341
                    <span class="n">hdr</span><span class="p">[</span><span class="n">elt</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">elt</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
342
                <span class="k">elif</span> <span class="n">elt</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;i&#39;</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
343
344
345
346
                    <span class="n">hdr</span><span class="p">[</span><span class="n">elt</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">elt</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">hdr</span><span class="p">[</span><span class="n">elt</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">elt</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>

Anthony Larcher's avatar
doc    
Anthony Larcher committed
347
348
349
        <span class="k">if</span> <span class="s1">&#39;sample_byte_format&#39;</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">hdr</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
            <span class="k">if</span> <span class="n">hdr</span><span class="p">[</span><span class="s1">&#39;sample_byte_format&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;0&#39;</span><span class="p">:</span>
                <span class="n">bord</span> <span class="o">=</span> <span class="s1">&#39;l&#39;</span>
Anthony Larcher's avatar
Anthony Larcher committed
350
            <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
351
352
353
                <span class="n">bord</span> <span class="o">=</span> <span class="s1">&#39;b&#39;</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">bord</span> <span class="o">!=</span> <span class="n">byteorder</span><span class="p">)</span> <span class="o">&amp;</span> <span class="nb">all</span><span class="p">([</span><span class="n">m</span> <span class="o">!=</span> <span class="s1">&#39;b&#39;</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">mode</span><span class="p">])</span> \
                    <span class="o">&amp;</span> <span class="nb">all</span><span class="p">([</span><span class="n">m</span> <span class="o">!=</span> <span class="s1">&#39;l&#39;</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">mode</span><span class="p">]):</span>
Anthony Larcher's avatar
Anthony Larcher committed
354
                <span class="n">byteorder</span> <span class="o">=</span> <span class="n">bord</span>
Anthony Larcher's avatar
Anthony Larcher committed
355

Anthony Larcher's avatar
doc    
Anthony Larcher committed
356
357
358
        <span class="n">icode</span> <span class="o">=</span> <span class="mi">0</span>  <span class="c1"># Get encoding, default is PCM</span>
        <span class="k">if</span> <span class="s1">&#39;sample_coding&#39;</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">hdr</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
            <span class="n">icode</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>  <span class="c1"># unknown code</span>
Anthony Larcher's avatar
Anthony Larcher committed
359
            <span class="k">for</span> <span class="n">coding</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">codings</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
360
361
362
363
                <span class="k">if</span> <span class="n">hdr</span><span class="p">[</span><span class="s1">&#39;sample_coding&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">coding</span><span class="p">):</span>
                    <span class="c1"># is the signal compressed</span>
                    <span class="c1"># if len(hdr[&#39;sample_coding&#39;]) &gt; codings[coding]:</span>
                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">hdr</span><span class="p">[</span><span class="s1">&#39;sample_coding&#39;</span><span class="p">])</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">coding</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
364
                        <span class="k">for</span> <span class="n">compression</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">compressions</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
365
                            <span class="k">if</span> <span class="n">hdr</span><span class="p">[</span><span class="s1">&#39;sample_coding&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="n">compression</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
366
367
368
                                <span class="n">icode</span> <span class="o">=</span> <span class="mi">10</span> <span class="o">*</span> <span class="n">compressions</span><span class="p">[</span><span class="n">compression</span><span class="p">]</span> \
                                        <span class="o">+</span> <span class="n">codings</span><span class="p">[</span><span class="n">coding</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span>
                                <span class="k">break</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
369
                    <span class="k">else</span><span class="p">:</span>  <span class="c1"># if the signal is not compressed</span>
Anthony Larcher's avatar
Anthony Larcher committed
370
                        <span class="n">icode</span> <span class="o">=</span> <span class="n">codings</span><span class="p">[</span><span class="n">coding</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span>
Anthony Larcher's avatar
Anthony Larcher committed
371
                        <span class="k">break</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
372
        <span class="c1"># initialize info of the files with default values</span>
Anthony Larcher's avatar
Anthony Larcher committed
373
        <span class="n">info</span> <span class="o">=</span> <span class="p">[</span><span class="n">fid</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">hlen</span><span class="p">,</span> <span class="nb">ord</span><span class="p">(</span><span class="n">byteorder</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">icode</span><span class="p">]</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
        <span class="c1"># Get existing info from the header</span>
        <span class="k">if</span> <span class="s1">&#39;sample_count&#39;</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">hdr</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
            <span class="n">info</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="n">hdr</span><span class="p">[</span><span class="s1">&#39;sample_count&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">info</span><span class="p">[</span><span class="mi">4</span><span class="p">]:</span>  <span class="c1"># if no info sample_count or zero</span>
            <span class="c1"># go to the end of the file</span>
            <span class="n">fid</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>  <span class="c1"># Go to te end of the file</span>
            <span class="c1"># get the sample count</span>
            <span class="n">info</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">((</span><span class="n">fid</span><span class="o">.</span><span class="n">tell</span><span class="p">()</span> <span class="o">-</span> <span class="n">info</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="o">/</span> <span class="p">(</span><span class="n">info</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="o">*</span> <span class="n">info</span><span class="p">[</span><span class="mi">6</span><span class="p">])))</span>  <span class="c1"># get the sample_count</span>
        <span class="k">if</span> <span class="s1">&#39;channel_count&#39;</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">hdr</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
            <span class="n">info</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="o">=</span> <span class="n">hdr</span><span class="p">[</span><span class="s1">&#39;channel_count&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="s1">&#39;sample_n_bytes&#39;</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">hdr</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
            <span class="n">info</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span> <span class="o">=</span> <span class="n">hdr</span><span class="p">[</span><span class="s1">&#39;sample_n_bytes&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="s1">&#39;sample_sig_bits&#39;</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">hdr</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
            <span class="n">info</span><span class="p">[</span><span class="mi">7</span><span class="p">]</span> <span class="o">=</span> <span class="n">hdr</span><span class="p">[</span><span class="s1">&#39;sample_sig_bits&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="s1">&#39;sample_rate&#39;</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">hdr</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
            <span class="n">info</span><span class="p">[</span><span class="mi">8</span><span class="p">]</span> <span class="o">=</span> <span class="n">hdr</span><span class="p">[</span><span class="s1">&#39;sample_rate&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="s1">&#39;sample_min&#39;</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">hdr</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
            <span class="n">info</span><span class="p">[</span><span class="mi">9</span><span class="p">]</span> <span class="o">=</span> <span class="n">hdr</span><span class="p">[</span><span class="s1">&#39;sample_min&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="s1">&#39;sample_max&#39;</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">hdr</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
            <span class="n">info</span><span class="p">[</span><span class="mi">10</span><span class="p">]</span> <span class="o">=</span> <span class="n">hdr</span><span class="p">[</span><span class="s1">&#39;sample_max&#39;</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
394
395
396
397
398
399
400

        <span class="n">ffx</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">hdr</span>
        <span class="n">ffx</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">info</span>
    <span class="n">info</span> <span class="o">=</span> <span class="n">ffx</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
    <span class="n">ksamples</span> <span class="o">=</span> <span class="n">info</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">ksamples</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">fid</span> <span class="o">=</span> <span class="n">info</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
401
402
403
        <span class="k">if</span> <span class="p">(</span><span class="n">icode</span> <span class="o">&gt;=</span> <span class="mi">10</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">ffx</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">):</span>  <span class="c1"># read compressed signal</span>
            <span class="c1"># need to use a script with SHORTEN</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;compressed signal, need to unpack in a script with SHORTEN&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
404
        <span class="n">info</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">ksamples</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
405
406
        <span class="c1"># use modes o and n to determine effective peak</span>
        <span class="n">pk</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">**</span> <span class="p">(</span><span class="mi">8</span> <span class="o">*</span> <span class="n">info</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">mno</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">-</span> <span class="nb">int</span><span class="p">(</span><span class="nb">all</span><span class="p">([</span><span class="n">m</span> <span class="o">!=</span> <span class="s1">&#39;b&#39;</span>
Anthony Larcher's avatar
Anthony Larcher committed
407
408
409
                                                                      <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span>
                                                                      <span class="n">mode</span><span class="p">])))</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">**</span>
                                       <span class="n">info</span><span class="p">[</span><span class="mi">7</span><span class="p">])</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
410
        <span class="n">fid</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">1024</span><span class="p">)</span>  <span class="c1"># jump after the header</span>
Anthony Larcher's avatar
Anthony Larcher committed
411
412
413
        <span class="n">nsamples</span> <span class="o">=</span> <span class="n">info</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="o">*</span> <span class="n">ksamples</span>
        <span class="k">if</span> <span class="n">info</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">info</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
414
415
                <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Sphere i1 PCM&#39;</span><span class="p">)</span>
                <span class="n">y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">fromfile</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="n">endianess</span><span class="p">[</span><span class="n">byteorder</span><span class="p">]</span><span class="o">+</span><span class="s2">&quot;i1&quot;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
416
417
                <span class="k">if</span> <span class="n">info</span><span class="p">[</span><span class="mi">11</span><span class="p">]</span> <span class="o">%</span> <span class="mi">10</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">y</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">%</span> <span class="mi">2</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
418
                        <span class="n">y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">frombuffer</span><span class="p">(</span><span class="n">audioop</span><span class="o">.</span><span class="n">ulaw2lin</span><span class="p">(</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
419
                                <span class="n">numpy</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">y</span><span class="p">,</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;int8&#39;</span><span class="p">))),</span> <span class="mi">2</span><span class="p">),</span>
Anthony Larcher's avatar
Anthony Larcher committed
420
                                <span class="n">numpy</span><span class="o">.</span><span class="n">int16</span><span class="p">)[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">/</span><span class="mf">32768.</span>
Anthony Larcher's avatar
Anthony Larcher committed
421
                    <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
422
                        <span class="n">y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">frombuffer</span><span class="p">(</span><span class="n">audioop</span><span class="o">.</span><span class="n">ulaw2lin</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">numpy</span><span class="o">.</span><span class="n">int16</span><span class="p">)</span><span class="o">/</span><span class="mf">32768.</span>
Anthony Larcher's avatar
Anthony Larcher committed
423
424
425
426
                    <span class="n">pk</span> <span class="o">=</span> <span class="mf">1.</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">y</span> <span class="o">=</span> <span class="n">y</span> <span class="o">-</span> <span class="mi">128</span>
            <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
427
428
429
                <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Sphere i2&#39;</span><span class="p">)</span>
                <span class="n">y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">fromfile</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="n">endianess</span><span class="p">[</span><span class="n">byteorder</span><span class="p">]</span><span class="o">+</span><span class="s2">&quot;i2&quot;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>  <span class="c1"># non verifie</span>
Anthony Larcher's avatar
Anthony Larcher committed
430
            <span class="k">if</span> <span class="n">info</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">4</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
431
                <span class="n">y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">fromfile</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="n">endianess</span><span class="p">[</span><span class="n">byteorder</span><span class="p">]</span><span class="o">+</span><span class="s2">&quot;i1&quot;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
432
                <span class="n">y</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">nsamples</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">transpose</span><span class="p">()</span>
Anthony Larcher's avatar
Anthony Larcher committed
433
                <span class="n">y</span> <span class="o">=</span> <span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">256</span><span class="p">,</span> <span class="mi">65536</span><span class="p">]),</span> <span class="n">y</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="p">:],</span> <span class="mi">2</span> <span class="o">**</span> <span class="p">(</span><span class="o">-</span><span class="mi">7</span><span class="p">))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">**</span> <span class="mi">24</span><span class="p">))</span>
Anthony Larcher's avatar
Anthony Larcher committed
434
            <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
435
                <span class="n">y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">fromfile</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="n">endianess</span><span class="p">[</span><span class="n">byteorder</span><span class="p">]</span><span class="o">+</span><span class="s2">&quot;i4&quot;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
436

Anthony Larcher's avatar
doc    
Anthony Larcher committed
437
438
        <span class="k">if</span> <span class="n">sc</span> <span class="o">!=</span> <span class="s1">&#39;r&#39;</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">sc</span> <span class="o">==</span> <span class="s1">&#39;s&#39;</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
439
                <span class="k">if</span> <span class="n">info</span><span class="p">[</span><span class="mi">9</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">info</span><span class="p">[</span><span class="mi">10</span><span class="p">]:</span>
Anthony Larcher's avatar
Anthony Larcher committed
440
441
                    <span class="n">info</span><span class="p">[</span><span class="mi">9</span><span class="p">]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
                    <span class="n">info</span><span class="p">[</span><span class="mi">10</span><span class="p">]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
442
                <span class="n">sf</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">/</span> <span class="n">numpy</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">abs</span><span class="p">,</span> <span class="n">info</span><span class="p">[</span><span class="mi">9</span><span class="p">:</span><span class="mi">11</span><span class="p">]))),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
443
444
445
446
447
448
449
            <span class="k">else</span><span class="p">:</span>
                <span class="n">sf</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">/</span> <span class="n">pk</span>
            <span class="n">y</span> <span class="o">=</span> <span class="n">sf</span> <span class="o">*</span> <span class="n">y</span>

        <span class="k">if</span> <span class="n">info</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">y</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">ksamples</span><span class="p">,</span> <span class="n">info</span><span class="p">[</span><span class="mi">5</span><span class="p">])</span>
    <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
450
        <span class="n">y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">([])</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
451
    <span class="k">if</span> <span class="n">mode</span> <span class="o">!=</span> <span class="s1">&#39;f&#39;</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
452
453
        <span class="n">fid</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="n">info</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
454
455
456
        <span class="k">if</span> <span class="ow">not</span> <span class="n">ffx</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="k">pass</span>  <span class="c1"># VERIFY SCRIPT, WHICH CASE IS HANDLED HERE</span>
    <span class="k">return</span> <span class="n">y</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">float32</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">info</span><span class="p">[</span><span class="mi">8</span><span class="p">]),</span> <span class="nb">int</span><span class="p">(</span><span class="n">info</span><span class="p">[</span><span class="mi">6</span><span class="p">])</span></div>
Anthony Larcher's avatar
Anthony Larcher committed
457

Anthony Larcher's avatar
doc    
Anthony Larcher committed
458

Anthony Larcher's avatar
Anthony Larcher committed
459
<div class="viewcode-block" id="read_audio"><a class="viewcode-back" href="../../api/frontend/io.html#frontend.io.read_audio">[docs]</a><span class="k">def</span> <span class="nf">read_audio</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">,</span> <span class="n">framerate</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
460
461
    <span class="sd">&quot;&quot;&quot; Read a 1 or 2-channel audio file in SPHERE, WAVE or RAW PCM format.</span>
<span class="sd">    The format is determined from the file extension.</span>
Anthony Larcher's avatar
Anthony Larcher committed
462
463
<span class="sd">    If the sample rate read from the file is a multiple of the one given</span>
<span class="sd">    as parameter, we apply a decimation function to subsample the signal.</span>
Anthony Larcher's avatar
Anthony Larcher committed
464
<span class="sd">    </span>
Anthony Larcher's avatar
Anthony Larcher committed
465
<span class="sd">    :param input_file_name: name of the file to read from</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
466
<span class="sd">    :param framerate: frame rate, optional, if lower than the one read from the file, subsampling is applied</span>
Anthony Larcher's avatar
Anthony Larcher committed
467
468
<span class="sd">    :return: the signal as a numpy array and the sampling frequency</span>
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
469
470
    <span class="k">if</span> <span class="n">framerate</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Expected sampling frequency required in sidekit.frontend.io.read_audio&quot;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
471
    <span class="n">ext</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
472
473
474
    <span class="k">if</span> <span class="n">ext</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;.sph&#39;</span><span class="p">:</span>
        <span class="n">sig</span><span class="p">,</span> <span class="n">read_framerate</span><span class="p">,</span> <span class="n">sampwidth</span> <span class="o">=</span> <span class="n">read_sph</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">,</span> <span class="s1">&#39;p&#39;</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">ext</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;.wav&#39;</span> <span class="ow">or</span> <span class="n">ext</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;.wave&#39;</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
475
        <span class="n">sig</span><span class="p">,</span> <span class="n">read_framerate</span><span class="p">,</span> <span class="n">sampwidth</span> <span class="o">=</span> <span class="n">read_wav</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
476
    <span class="k">elif</span> <span class="n">ext</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;.pcm&#39;</span> <span class="ow">or</span> <span class="n">ext</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;.raw&#39;</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
477
478
        <span class="n">sig</span><span class="p">,</span> <span class="n">read_framerate</span><span class="p">,</span> <span class="n">sampwidth</span> <span class="o">=</span> <span class="n">read_pcm</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">)</span>
        <span class="n">read_framerate</span> <span class="o">=</span> <span class="n">framerate</span>
Anthony Larcher's avatar
Anthony Larcher committed
479
    <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
480
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Unknown extension of audio file&quot;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
481

Anthony Larcher's avatar
doc    
Anthony Larcher committed
482
    <span class="c1"># Convert to 16 bit encoding if needed</span>
Anthony Larcher's avatar
Anthony Larcher committed
483
484
485
    <span class="n">sig</span> <span class="o">*=</span> <span class="p">(</span><span class="mi">2</span><span class="o">**</span><span class="p">(</span><span class="mi">15</span><span class="o">-</span><span class="n">sampwidth</span><span class="p">))</span>

    <span class="k">if</span> <span class="n">framerate</span> <span class="o">&gt;</span> <span class="n">read_framerate</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
486
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Warning in read_audio, up-sampling function is not implemented yet!&quot;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
487
    <span class="k">elif</span> <span class="n">read_framerate</span> <span class="o">%</span> <span class="nb">float</span><span class="p">(</span><span class="n">framerate</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">framerate</span> <span class="o">==</span> <span class="n">read_framerate</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
488
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;downsample&quot;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
489
        <span class="n">sig</span> <span class="o">=</span> <span class="n">scipy</span><span class="o">.</span><span class="n">signal</span><span class="o">.</span><span class="n">decimate</span><span class="p">(</span><span class="n">sig</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">read_framerate</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">framerate</span><span class="p">)),</span> <span class="n">n</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">ftype</span><span class="o">=</span><span class="s1">&#39;iir&#39;</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
490
491
    <span class="k">return</span> <span class="n">sig</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">float32</span><span class="p">),</span> <span class="n">framerate</span></div>

Anthony Larcher's avatar
Anthony Larcher committed
492

Anthony Larcher's avatar
update    
Anthony Larcher committed
493
494
<span class="nd">@check_path_existance</span>
<span class="k">def</span> <span class="nf">write_label</span><span class="p">(</span><span class="n">label</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
495
                <span class="n">output_file_name</span><span class="p">,</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
496
                <span class="n">selected_label</span><span class="o">=</span><span class="s1">&#39;speech&#39;</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
497
                <span class="n">frame_per_second</span><span class="o">=</span><span class="mi">100</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
498
499
    <span class="sd">&quot;&quot;&quot;Save labels in ALIZE format</span>

Anthony Larcher's avatar
Anthony Larcher committed
500
<span class="sd">    :param output_file_name: name of the file to write to</span>
Anthony Larcher's avatar
Anthony Larcher committed
501
<span class="sd">    :param label: label to write in the file given as a ndarray of boolean</span>
Anthony Larcher's avatar
Anthony Larcher committed
502
503
<span class="sd">    :param selected_label: label to write to the file. Default is &#39;speech&#39;.</span>
<span class="sd">    :param frame_per_second: number of frame per seconds. Used to convert</span>
Anthony Larcher's avatar
Anthony Larcher committed
504
505
<span class="sd">            the frame number into time. Default is 100.</span>
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
506
507
    <span class="k">if</span> <span class="n">label</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">bits</span> <span class="o">=</span> <span class="n">label</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">^</span> <span class="n">label</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
508
509
        <span class="c1"># convert true value into a list of feature indexes</span>
        <span class="c1"># append 0 at the beginning of the list, append the last index to the list</span>
Anthony Larcher's avatar
Anthony Larcher committed
510
511
        <span class="n">idx</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">bits</span><span class="p">))[</span><span class="n">bits</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span> <span class="o">+</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">label</span><span class="p">)]</span>
        <span class="n">framerate</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="n">frame_per_second</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
512
513
        <span class="c1"># for each pair of indexes (idx[i] and idx[i+1]), create a segment</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">output_file_name</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fid</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
514
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="o">~</span><span class="n">label</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">len</span><span class="p">(</span><span class="n">idx</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
515
                <span class="n">fid</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> </span><span class="si">{}</span><span class="s1"> </span><span class="si">{}</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">idx</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">framerate</span><span class="p">),</span>
Anthony Larcher's avatar
Anthony Larcher committed
516
                                              <span class="nb">str</span><span class="p">(</span><span class="n">idx</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="n">framerate</span><span class="p">),</span> <span class="n">selected_label</span><span class="p">))</span>
Anthony Larcher's avatar
update    
Anthony Larcher committed
517

Anthony Larcher's avatar
Anthony Larcher committed
518

Anthony Larcher's avatar
Anthony Larcher committed
519
<div class="viewcode-block" id="read_label"><a class="viewcode-back" href="../../api/frontend/io.html#frontend.io.read_label">[docs]</a><span class="k">def</span> <span class="nf">read_label</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">,</span> <span class="n">selected_label</span><span class="o">=</span><span class="s1">&#39;speech&#39;</span><span class="p">,</span> <span class="n">frame_per_second</span><span class="o">=</span><span class="mi">100</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
520
521
    <span class="sd">&quot;&quot;&quot;Read label file in ALIZE format</span>

Anthony Larcher's avatar
Anthony Larcher committed
522
523
524
<span class="sd">    :param input_file_name: the label file name</span>
<span class="sd">    :param selected_label: the label to return. Default is &#39;speech&#39;.</span>
<span class="sd">    :param frame_per_second: number of frame per seconds. Used to convert</span>
Anthony Larcher's avatar
Anthony Larcher committed
525
526
527
528
<span class="sd">            the frame number into time. Default is 100.</span>

<span class="sd">    :return: a logical array</span>
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
529
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
530
531
        <span class="n">segments</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>

Anthony Larcher's avatar
update    
Anthony Larcher committed
532
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">segments</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
533
        <span class="n">lbl</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">bool</span><span class="p">)</span>
Anthony Larcher's avatar
update    
Anthony Larcher committed
534
    <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
535
        <span class="c1"># initialize the length from the last segment&#39;s end</span>
Anthony Larcher's avatar
update    
Anthony Larcher committed
536
        <span class="n">foo1</span><span class="p">,</span> <span class="n">stop</span><span class="p">,</span> <span class="n">foo2</span> <span class="o">=</span> <span class="n">segments</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
Anthony Larcher's avatar
Anthony Larcher committed
537
        <span class="n">lbl</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">stop</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">bool</span><span class="p">)</span>
Anthony Larcher's avatar
update    
Anthony Larcher committed
538
    
Anthony Larcher's avatar
Anthony Larcher committed
539
540
        <span class="n">begin</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">segments</span><span class="p">))</span>
        <span class="n">end</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">segments</span><span class="p">))</span>
Anthony Larcher's avatar
update    
Anthony Larcher committed
541
542
543
    
        <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">segments</span><span class="p">)):</span>
            <span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span> <span class="n">segments</span><span class="p">[</span><span class="n">s</span><span class="p">]</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
Anthony Larcher's avatar
Anthony Larcher committed
544
545
546
            <span class="k">if</span> <span class="n">label</span> <span class="o">==</span> <span class="n">selected_label</span><span class="p">:</span>
                <span class="n">begin</span><span class="p">[</span><span class="n">s</span><span class="p">]</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">start</span><span class="p">)</span> <span class="o">*</span> <span class="n">frame_per_second</span><span class="p">))</span>
                <span class="n">end</span><span class="p">[</span><span class="n">s</span><span class="p">]</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">stop</span><span class="p">)</span> <span class="o">*</span> <span class="n">frame_per_second</span><span class="p">))</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
547
548
                <span class="n">lbl</span><span class="p">[</span><span class="n">begin</span><span class="p">[</span><span class="n">s</span><span class="p">]:</span><span class="n">end</span><span class="p">[</span><span class="n">s</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="k">return</span> <span class="n">lbl</span></div>
Anthony Larcher's avatar
Anthony Larcher committed
549

Anthony Larcher's avatar
Anthony Larcher committed
550

Anthony Larcher's avatar
Anthony Larcher committed
551
<div class="viewcode-block" id="read_spro4"><a class="viewcode-back" href="../../api/frontend/io.html#frontend.io.read_spro4">[docs]</a><span class="k">def</span> <span class="nf">read_spro4</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">,</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
552
553
               <span class="n">label_file_name</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span>
               <span class="n">selected_label</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
554
               <span class="n">frame_per_second</span><span class="o">=</span><span class="mi">100</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
555
556
    <span class="sd">&quot;&quot;&quot;Read a feature stream in SPRO4 format </span>
<span class="sd">    </span>
Anthony Larcher's avatar
Anthony Larcher committed
557
558
<span class="sd">    :param input_file_name: name of the feature file to read from</span>
<span class="sd">    :param label_file_name: name of the label file to read if required.</span>
Anthony Larcher's avatar
Anthony Larcher committed
559
<span class="sd">        By Default, the method assumes no label to read from.    </span>
Anthony Larcher's avatar
Anthony Larcher committed
560
561
<span class="sd">    :param selected_label: label to select in the label file. Default is none.</span>
<span class="sd">    :param frame_per_second: number of frame per seconds. Used to convert</span>
Anthony Larcher's avatar
Anthony Larcher committed
562
563
<span class="sd">            the frame number into time. Default is 0.</span>
<span class="sd">    </span>
Anthony Larcher's avatar
Anthony Larcher committed
564
<span class="sd">    :return: a sequence of features in a numpy array</span>
Anthony Larcher's avatar
Anthony Larcher committed
565
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
566
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">input_file_name</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
567

Anthony Larcher's avatar
doc    
Anthony Larcher committed
568
        <span class="n">tmp_s</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s2">&quot;8c&quot;</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">8</span><span class="p">))</span>
Anthony Larcher's avatar
Anthony Larcher committed
569
570
        <span class="n">s</span> <span class="o">=</span> <span class="p">()</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">tmp_s</span><span class="p">)):</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
571
572
573
574
            <span class="n">s</span> <span class="o">+=</span> <span class="p">(</span><span class="n">tmp_s</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">&quot;utf-8&quot;</span><span class="p">),)</span>
        <span class="n">f</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>  <span class="c1"># Go to te end of the file</span>
        <span class="n">size</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">tell</span><span class="p">()</span>  <span class="c1"># get the position</span>
        <span class="n">f</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>  <span class="c1"># go back to the begining of the file</span>
Anthony Larcher's avatar
Anthony Larcher committed
575
        <span class="n">head_size</span> <span class="o">=</span> <span class="mi">0</span>
Anthony Larcher's avatar
Anthony Larcher committed
576

Anthony Larcher's avatar
doc    
Anthony Larcher committed
577
578
579
        <span class="k">if</span> <span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">==</span> <span class="s1">&#39;&lt;header&gt;&#39;</span><span class="p">:</span>
            <span class="c1"># swap empty header for general header the code need changing</span>
            <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s2">&quot;19b&quot;</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">19</span><span class="p">))</span>
Anthony Larcher's avatar
Anthony Larcher committed
580
            <span class="n">head_size</span> <span class="o">=</span> <span class="mi">19</span>
Anthony Larcher's avatar
Anthony Larcher committed
581

Anthony Larcher's avatar
doc    
Anthony Larcher committed
582
583
584
        <span class="n">dim</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s2">&quot;H&quot;</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">2</span><span class="p">))[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s2">&quot;4b&quot;</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
        <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s2">&quot;f&quot;</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
Anthony Larcher's avatar
Anthony Larcher committed
585
        <span class="n">n_frames</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">((</span><span class="n">size</span> <span class="o">-</span> <span class="mi">10</span> <span class="o">-</span> <span class="n">head_size</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="mi">4</span> <span class="o">*</span> <span class="n">dim</span><span class="p">)))</span>
Anthony Larcher's avatar
Anthony Larcher committed
586

Anthony Larcher's avatar
doc    
Anthony Larcher committed
587
        <span class="n">features</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;f&#39;</span> <span class="o">*</span> <span class="n">n_frames</span> <span class="o">*</span> <span class="n">dim</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
588
589
                                               <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">4</span> <span class="o">*</span> <span class="n">n_frames</span> <span class="o">*</span> <span class="n">dim</span><span class="p">)))</span>
        <span class="n">features</span><span class="o">.</span><span class="n">resize</span><span class="p">((</span><span class="n">n_frames</span><span class="p">,</span> <span class="n">dim</span><span class="p">))</span>
Anthony Larcher's avatar
Anthony Larcher committed
590

Anthony Larcher's avatar
Anthony Larcher committed
591
    <span class="n">lbl</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">features</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">bool</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
592
    <span class="k">if</span> <span class="ow">not</span> <span class="n">label_file_name</span> <span class="o">==</span> <span class="s2">&quot;&quot;</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
593
        <span class="n">lbl</span> <span class="o">=</span> <span class="n">read_label</span><span class="p">(</span><span class="n">label_file_name</span><span class="p">,</span> <span class="n">selected_label</span><span class="p">,</span> <span class="n">frame_per_second</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
594
595

    <span class="n">features</span> <span class="o">=</span> <span class="n">features</span><span class="p">[</span><span class="n">lbl</span><span class="p">,</span> <span class="p">:]</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
596
    <span class="k">return</span> <span class="n">features</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span></div>
Anthony Larcher's avatar
Anthony Larcher committed
597

Anthony Larcher's avatar
Anthony Larcher committed
598

Anthony Larcher's avatar
Anthony Larcher committed
599
600
601
602
603
604
<div class="viewcode-block" id="read_hdf5_segment"><a class="viewcode-back" href="../../api/frontend/io.html#frontend.io.read_hdf5_segment">[docs]</a><span class="k">def</span> <span class="nf">read_hdf5_segment</span><span class="p">(</span><span class="n">file_handler</span><span class="p">,</span>
                      <span class="n">show</span><span class="p">,</span>
                      <span class="n">dataset_list</span><span class="p">,</span>
                      <span class="n">label</span><span class="p">,</span>
                      <span class="n">start</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                      <span class="n">global_cmvn</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
605
606
607
608
609
610
611
612
613
614
615
616
617
    <span class="sd">&quot;&quot;&quot;Read a segment from a stream in HDF5 format. Return the features in the</span>
<span class="sd">    range start:end</span>
<span class="sd">    In case the start and end cannot be reached, the first or last feature are copied</span>
<span class="sd">    so that the length of the returned segment is always end-start</span>

<span class="sd">    :param file_name: name of the file to open</span>
<span class="sd">    :param dataset: identifier of the dataset in the HDF5 file</span>
<span class="sd">    :param mask:</span>
<span class="sd">    :param start:</span>
<span class="sd">    :param end:</span>

<span class="sd">    :return:read_hdf5_segment</span>
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
618
    <span class="n">h5f</span> <span class="o">=</span> <span class="n">file_handler</span>
Anthony Larcher's avatar
Anthony Larcher committed
619

Anthony Larcher's avatar
Anthony Larcher committed
620
621
622
623
624
625
    <span class="n">compression_type</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">:</span> <span class="s1">&#39;none&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">:</span> <span class="s1">&#39;htk&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">:</span> <span class="s1">&#39;percentile&#39;</span><span class="p">}</span>
    <span class="k">if</span> <span class="s2">&quot;compression&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">h5f</span><span class="p">:</span>
        <span class="n">compression</span> <span class="o">=</span> <span class="s1">&#39;none&#39;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Warning, default feature storage mode is now using compression&quot;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">compression</span> <span class="o">=</span> <span class="n">compression_type</span><span class="p">[</span><span class="n">h5f</span><span class="p">[</span><span class="s2">&quot;compression&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">value</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
626

Anthony Larcher's avatar
Anthony Larcher committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
    <span class="k">if</span> <span class="n">show</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">h5f</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;show </span><span class="si">{}</span><span class="s1"> is not in the HDF5 file&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">show</span><span class="p">))</span>