import scipy, pylab
from scipy.io import wavfile
from scipy import signal
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import os
import webbrowser
try:
    from future import *
except:
    pass
"""Komori visualization subpackage"""

def version():
    return '0.20'

class recording:
    """ Recording class

    path - path to WAV file

    example:

    r = recording(path = '/path/to/file.wav')

    """
    def __init__(self, path):
        self.path = path
        self.samplerate, self.data = wavfile.read(self.path)
        self.duration = float(self.data.size)/self.samplerate

    def resample(self, et=10):
        """
        Resampling on numpy arrays
        Domagoj

        data: numpy array of data recorded with sr samplerate
        sr: recording samplerate
        et: given time expansion factor

        saves resampled.wav into current directory
        """
        rep_sr = 44100
        sr = self.samplerate/et
        secs = self.data.size/sr
        samps = secs * rep_sr
        resampled = signal.resample(self.data, samps)
        # print resampled, 'resampled'
        # TODO: path must be better specified
        wavfile.write('tmp/'+'resampled.wav', 44100, resampled)

    def play(self, et=10):
        """
        Plays the recording with default mediaplayer resampled for human hearing
        """
        self.resample(et)
        webbrowser.open(os.path.abspath("tmp/resampled.wav"))


class spectrogram: