#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# CREATED:2015-02-14 19:13:49 by Brian McFee <brian.mcfee@nyu.edu>
'''Unit tests for time and frequency conversion'''

import os
try:
    os.environ.pop('LIBROSA_CACHE_DIR')
except KeyError:
    pass

import matplotlib
matplotlib.use('Agg')

import librosa
import numpy as np
from nose.tools import raises, eq_


def test_frames_to_samples():

    def __test(x, y, hop_length, n_fft):
        y_test = librosa.frames_to_samples(x,
                                           hop_length=hop_length,
                                           n_fft=n_fft)
        assert np.allclose(y, y_test)

    x = np.arange(2)
    for hop_length in [512, 1024]:
        for n_fft in [None, 1024]:
            y = x * hop_length
            if n_fft is not None:
                y += n_fft // 2
            yield __test, x, y, hop_length, n_fft


def test_samples_to_frames():

    def __test(x, y, hop_length, n_fft):
        y_test = librosa.samples_to_frames(x,
                                           hop_length=hop_length,
                                           n_fft=n_fft)
        assert np.allclose(y, y_test)

    x = np.arange(2)
    for hop_length in [512, 1024]:
        for n_fft in [None, 1024]:
            y = x * hop_length
            if n_fft is not None:
                y += n_fft // 2
            yield __test, y, x, hop_length, n_fft