__author__ = 'Jesus Maria'

import numpy as np
import wave
import struct

def read(file_path):
    """
    Inputs:
        file_path (string) = path for the file we want to get samples from.
        It has to be a WAV file with just one channel (mono)
    Output:
        The function should return an array with the file's samples
        samples (numpy array) = The resulting array of samples
    """

    # Open file for read-only
    file_content = wave.open(file_path, 'r')

    # Extract Raw Audio from Wav File
    samples = file_content.readframes(-1)
    samples = np.fromstring(samples, 'Int16')

    # If Stereo, do not return
    if file_content.getnchannels() == 2:
        return []
    else:
        return samples


def write(samples, file_path):

    output_file = wave.open(file_path, 'w')
    output_file.setparams((2, 2, 44100, 0, 'NONE', 'not compressed'))

    for i in range(0, len(samples)):
        packed_value = struct.pack('h', samples[i])
        output_file.writeframes(packed_value)
        output_file.writeframes(packed_value)

    output_file.close()