# Path: dunyadesktop_app/widgets/widgetutilities.py
# def downsample_plot(plot_array, ds_limit):
#     # Decide by how much we should downsample
#     """
#     Downsamples the given pitch array according to the given downsample limit.
# 
#     :param plot_array: (numpy array) A sequence of pitch values
#                        [440.4, 442.3, 440.0, ...]
#     :param ds_limit: (int) Maximum number of samples to be plotted.
# 
#     :return: (numpy array) A sequence of pitch values
#     """
#     size_array = np.size(plot_array)
#     ds = int(size_array / ds_limit) + 1
# 
#     if ds == 1:
#         # Small enough to display with no intervention.
#         return plot_array
#     else:
#         # Here convert data into a down-sampled array suitable for
#         # visualizing. Must do this piecewise to limit memory usage.
#         samples = 1 + (size_array // ds)
#         visible = np.zeros(samples * 2, dtype=plot_array.dtype)
#         source_ptr = 0
#         target_ptr = 0
# 
#         # read data in chunks of ~1M samples
#         chunk_size = (1000000 // ds) * ds
#         while source_ptr < size_array - 1:
#             chunk = plot_array[source_ptr:min(size_array,
#                                               source_ptr+chunk_size)]
#             size_chunk = np.size(chunk)
#             source_ptr += size_chunk
#             # reshape chunk to be integral multiple of ds
#             chunk = chunk[:(size_chunk // ds) * ds].reshape(size_chunk//ds, ds)
# 
#             # compute max and min
#             chunk_max = chunk.max(axis=1)
#             chunk_min = chunk.min(axis=1)
# 
#             # interleave min and max into plot data to preserve
#             # envelope shape
#             visible[target_ptr:target_ptr + chunk.shape[0] * 2:2] = chunk_min
#             visible[1+target_ptr:1+target_ptr+chunk.shape[0] * 2:2] = chunk_max
#             target_ptr += chunk.shape[0] * 2
#         plot_y = visible[:target_ptr]
#         plot_y[-1] = np.nan
#     return plot_y


from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import QCursor
from .widgetutilities import downsample_plot
import pyqtgraph as pg
import numpy as np


# Enable OpenGL and Weave to improve the performance of plotting functions.
pg.setConfigOptions(useOpenGL=True)
pg.setConfigOptions(useWeave=True)

# Colors of the waveform plot, vertical line (cursor) and waveform region
# selector
WAVEFORM_PEN = (20, 170, 100, 80)  # (Red, Green, Blue, a)
WAVEFORM_BRUSH = pg.mkBrush((50, 255, 255, 45))
WAVEFORM_VLINE = pg.mkPen((255, 40, 35, 150), cosmetic=True, width=2)



# Path: dunyadesktop_app/widgets/widgetutilities.py
# def downsample_plot(plot_array, ds_limit):
#     # Decide by how much we should downsample
#     """
#     Downsamples the given pitch array according to the given downsample limit.
# 
#     :param plot_array: (numpy array) A sequence of pitch values
#                        [440.4, 442.3, 440.0, ...]
#     :param ds_limit: (int) Maximum number of samples to be plotted.
# 
#     :return: (numpy array) A sequence of pitch values
#     """
#     size_array = np.size(plot_array)
#     ds = int(size_array / ds_limit) + 1
# 
#     if ds == 1:
#         # Small enough to display with no intervention.
#         return plot_array
#     else:
#         # Here convert data into a down-sampled array suitable for
#         # visualizing. Must do this piecewise to limit memory usage.
#         samples = 1 + (size_array // ds)
#         visible = np.zeros(samples * 2, dtype=plot_array.dtype)
#         source_ptr = 0
#         target_ptr = 0
# 
#         # read data in chunks of ~1M samples
#         chunk_size = (1000000 // ds) * ds
#         while source_ptr < size_array - 1:
#             chunk = plot_array[source_ptr:min(size_array,
#                                               source_ptr+chunk_size)]
#             size_chunk = np.size(chunk)
#             source_ptr += size_chunk
#             # reshape chunk to be integral multiple of ds
#             chunk = chunk[:(size_chunk // ds) * ds].reshape(size_chunk//ds, ds)
# 
#             # compute max and min
#             chunk_max = chunk.max(axis=1)
#             chunk_min = chunk.min(axis=1)
# 
#             # interleave min and max into plot data to preserve
#             # envelope shape
#             visible[target_ptr:target_ptr + chunk.shape[0] * 2:2] = chunk_min
#             visible[1+target_ptr:1+target_ptr+chunk.shape[0] * 2:2] = chunk_max
#             target_ptr += chunk.shape[0] * 2
#         plot_y = visible[:target_ptr]
#         plot_y[-1] = np.nan
#     return plot_y


# Path: dunyadesktop_app/widgets/waveformwidget.py
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import QCursor
from .widgetutilities import downsample_plot
import pyqtgraph as pg
import numpy as np



# Enable OpenGL and Weave to improve the performance of plotting functions.
pg.setConfigOptions(useOpenGL=True)
pg.setConfigOptions(useWeave=True)

# Colors of the waveform plot, vertical line (cursor) and waveform region
# selector
WAVEFORM_PEN = (20, 170, 100, 80)  # (Red, Green, Blue, a)
WAVEFORM_BRUSH = pg.mkBrush((50, 255, 255, 45))
WAVEFORM_VLINE = pg.mkPen((255, 40, 35, 150), cosmetic=True, width=2)



class WaveformRegionItem(pg.LinearRegionItem):