#!/usr/bin/env python
# encoding: utf-8

# The MIT License (MIT)

# Copyright (c) 2016 CNRS

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# AUTHORS
# Hervé BREDIN - http://herve.niderb.fr


from __future__ import division

import h5py
import numpy as np
import pysndfile.sndio
from pyannote.core import SlidingWindow, SlidingWindowFeature


class PyannoteFeatureExtractionError(Exception):
    pass


def get_wav_duration(wav):
    y, sample_rate, _ = pysndfile.sndio.read(wav)
    n_samples = y.shape[0]
    return n_samples / sample_rate


class Precomputed(object):
    """Load precomputed features from HDF5 file

    Parameters
    ----------
    features_h5 : str
        Path to HDF5 file generated by script 'feature_extraction.py'.
    """

    @staticmethod
    def get_config_path(root_dir):
        path = '{root_dir}/metadata.h5'.format(root_dir=root_dir)
        return path

    @staticmethod
    def get_path(root_dir, database=None, uri=None, channel=1, **kwargs):
        path = '{root_dir}'.format(root_dir=root_dir)
        if database is not None:
            path += '/{database}'.format(database=database)
        path += '/{uri}_{channel:d}.h5'.format(uri=uri, channel=channel)
        return path

    def __init__(self, root_dir=None):
        super(Precomputed, self).__init__()
        self.root_dir = root_dir

        path = self.get_config_path(self.root_dir)

        f = h5py.File(path)
        start = f.attrs['start']
        duration = f.attrs['duration']
        step = f.attrs['step']
        self.sliding_window_ = SlidingWindow(
            start=start, duration=duration, step=step)
        self.dimension_ = f.attrs['dimension']
        f.close()

    def sliding_window(self):
        return self.sliding_window_

    def dimension(self):
        return self.dimension_

    def __call__(self, wav, database=None, uri=None, channel=1, **kwargs):

        path = self.get_path(self.root_dir, database=database,
                             uri=uri, channel=channel)

        if not os.path.exists(path):
            msg = 'No precomputed features for "{wav}".'
            raise PyannoteFeatureExtractionError(msg.format(wav=wav))

        f = h5py.File(path)
        data = np.array(f['features'])
        f.close()
        return SlidingWindowFeature(data, self.sliding_window_)

@@ -1237,16 +1237,31 @@
 rt h5py%0A
+import os.path%0A
 import n
@@ -2854,19 +2854,24 @@
 _(self, 
-wav
+filename
 , databa
@@ -3117,19 +3117,24 @@
 s for %22%7B
-wav
+filename
 %7D%22.'%0A   
@@ -3194,15 +3194,25 @@
 mat(
-wav=wav
+filename=filename
 ))%0A%0A
@@ -3278,16 +3278,16 @@
 ures'%5D)%0A
-
         
@@ -3296,16 +3296,17 @@
 close()%0A
+%0A
         

fix missing import and multiple value kwargs