# Path: compmusic/extractors/imagelib/MelSpectrogramImage.py
# def create_wave_images(input_filename, output_filename_w, output_filename_s, output_filename_m, image_width,
#                        image_height, fft_size, progress_callback=None, f_min=None, f_max=None, scale_exp=None,
#                        pallete=None):
#     """
#     Utility function for creating both wavefile and spectrum images from an audio input file.
#     """
#     numMelBands = 26  # as in htk
#     processor = AudioProcessor(input_filename, fft_size, np.hanning)
#     inv_processor = InvMFCCAudioProcessor(input_filename, fft_size, numMelBands)
#     samples_per_pixel = processor.audio_file.nframes / float(image_width)
# 
#     waveform = WaveformImage(image_width, image_height)
#     spectrogram = SpectrogramImage(image_width, image_height, fft_size, f_min, f_max, scale_exp, pallete)
#     mel_spectrogram = MelSpectrogramImage(image_width, image_height, scale_exp, pallete, numMelBands)
# 
#     for x in range(image_width):
# 
#         if image_width >= 10:
#             if progress_callback and x % (image_width / 10) == 0:
#                 progress_callback((x * 100) / image_width)
# 
#         seek_point = int(x * samples_per_pixel)
#         next_seek_point = int((x + 1) * samples_per_pixel)
# 
#         (spectral_centroid, db_spectrum) = processor.spectral_centroid(seek_point)
#         peaks = processor.peaks(seek_point, next_seek_point)
# 
#         inv_mfcc_spectrum = inv_processor.compute_inv_mfcc(
#             seek_point)  # inv MFCC computation results in a mel spectrogram
#         waveform.draw_peaks(x, peaks, spectral_centroid)
#         spectrogram.draw_spectrum(x, db_spectrum)
#         mel_spectrogram.draw_spectrum(x, inv_mfcc_spectrum)
# 
#     if progress_callback:
#         progress_callback(100)
# 
#     waveform.save(output_filename_w)
#     spectrogram.save(output_filename_s)
#     mel_spectrogram.save(output_filename_m)


import sys
from compmusic.extractors.imagelib.MelSpectrogramImage import create_wave_images
from processing import AudioProcessingException
parser.add_option("-w", "--width", action="store", dest="image_width", type="int", help="image width in pixels (default %default)")
parser.add_option("-h", "--height", action="store", dest="image_height", type="int", help="image height in pixels (default %default)")
parser.add_option("-f", "--fft", action="store", dest="fft_size", type="int", help="fft size, power of 2 for increased performance (default %default)")
parser.add_option("-p", "--profile", action="store_true", dest="profile", help="run profiler and output profiling information")

parser.set_defaults(output_filename_w=None, output_filename_s=None, image_width=500, image_height=171, fft_size=2048)

(options, args) = parser.parse_args()

if len(args) == 0:
    parser.print_help()
    parser.error("not enough arguments")
   
    if len(args) > 1 and (options.output_filename_w != None or options.output_filename_s != None):
        parser.error("when processing multiple files you can't define the output filename!")
'''


def progress_callback(percentage):
    sys.stdout.write(str(percentage) + "% ")
    sys.stdout.flush()

    # process all files so the user can use wildcards like *.wav


def genimages(input_file, output_file_w, output_file_s, output_file_m, options):
    args = (input_file, output_file_w, output_file_s, output_file_m, options.image_width, options.image_height,
            options.fft_size, progress_callback, options.f_min, options.f_max, options.scale_exp, options.pallete)
    print("processing file %s:\n\t" % input_file, end="")
    try:

# Path: compmusic/extractors/imagelib/MelSpectrogramImage.py
# def create_wave_images(input_filename, output_filename_w, output_filename_s, output_filename_m, image_width,
#                        image_height, fft_size, progress_callback=None, f_min=None, f_max=None, scale_exp=None,
#                        pallete=None):
#     """
#     Utility function for creating both wavefile and spectrum images from an audio input file.
#     """
#     numMelBands = 26  # as in htk
#     processor = AudioProcessor(input_filename, fft_size, np.hanning)
#     inv_processor = InvMFCCAudioProcessor(input_filename, fft_size, numMelBands)
#     samples_per_pixel = processor.audio_file.nframes / float(image_width)
# 
#     waveform = WaveformImage(image_width, image_height)
#     spectrogram = SpectrogramImage(image_width, image_height, fft_size, f_min, f_max, scale_exp, pallete)
#     mel_spectrogram = MelSpectrogramImage(image_width, image_height, scale_exp, pallete, numMelBands)
# 
#     for x in range(image_width):
# 
#         if image_width >= 10:
#             if progress_callback and x % (image_width / 10) == 0:
#                 progress_callback((x * 100) / image_width)
# 
#         seek_point = int(x * samples_per_pixel)
#         next_seek_point = int((x + 1) * samples_per_pixel)
# 
#         (spectral_centroid, db_spectrum) = processor.spectral_centroid(seek_point)
#         peaks = processor.peaks(seek_point, next_seek_point)
# 
#         inv_mfcc_spectrum = inv_processor.compute_inv_mfcc(
#             seek_point)  # inv MFCC computation results in a mel spectrogram
#         waveform.draw_peaks(x, peaks, spectral_centroid)
#         spectrogram.draw_spectrum(x, db_spectrum)
#         mel_spectrogram.draw_spectrum(x, inv_mfcc_spectrum)
# 
#     if progress_callback:
#         progress_callback(100)
# 
#     waveform.save(output_filename_w)
#     spectrogram.save(output_filename_s)
#     mel_spectrogram.save(output_filename_m)


# Path: compmusic/extractors/imagelib/wav2png.py
import sys
from compmusic.extractors.imagelib.MelSpectrogramImage import create_wave_images
from processing import AudioProcessingException

parser.add_option("-w", "--width", action="store", dest="image_width", type="int", help="image width in pixels (default %default)")
parser.add_option("-h", "--height", action="store", dest="image_height", type="int", help="image height in pixels (default %default)")
parser.add_option("-f", "--fft", action="store", dest="fft_size", type="int", help="fft size, power of 2 for increased performance (default %default)")
parser.add_option("-p", "--profile", action="store_true", dest="profile", help="run profiler and output profiling information")

parser.set_defaults(output_filename_w=None, output_filename_s=None, image_width=500, image_height=171, fft_size=2048)

(options, args) = parser.parse_args()

if len(args) == 0:
    parser.print_help()
    parser.error("not enough arguments")
   
    if len(args) > 1 and (options.output_filename_w != None or options.output_filename_s != None):
        parser.error("when processing multiple files you can't define the output filename!")
'''


def progress_callback(percentage):
    sys.stdout.write(str(percentage) + "% ")
    sys.stdout.flush()

    # process all files so the user can use wildcards like *.wav


def genimages(input_file, output_file_w, output_file_s, output_file_m, options):
    args = (input_file, output_file_w, output_file_s, output_file_m, options.image_width, options.image_height,
            options.fft_size, progress_callback, options.f_min, options.f_max, options.scale_exp, options.pallete)
    print("processing file %s:\n\t" % input_file, end="")
    try:

        create_wave_images(*args)