#!/usr/bin/python

# ----------------------------------------------------------------------------
#
#  Copyright (C) 2020-2022 Fons Adriaensen <fons@linuxaudio.org>
#    
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http:#www.gnu.org/licenses/>.
#
# ----------------------------------------------------------------------------


# Kmeters for Ambisonics signals up to degree 8.

import sys
import signal
from PyQt5 import QtGui, QtCore, QtWidgets
from zita_jacktools.jackkmeter import *
from sys import argv
sys.path.append ('..')
from utils.kmeters import *


class Meterwin(QtWidgets.QWidget):
    
    def __init__(self, nchan, metrics):
        super(Meterwin, self).__init__()
        self.metrics = metrics
        mlist = [ nchan ]
        self.disp = K20meter(self, metrics, mlist, 6, 6)
        self.disp.move(0, 0)
        self.disp.show()
        self.setFixedSize (self.disp.sx, self.disp.sy)
        self.setWindowTitle('Ambisonic levels')
        self.show()        
        self.timer = QtCore.QBasicTimer ()
        self.timer.start (50, self)

    def timerEvent (self, ev):
        st, rms, dpk = K20.get_levels ()
        self.disp.rms = rms
        self.disp.pks = dpk
        self.disp.update ()

if len (argv) < 2:
    print ("mcmeter <channels>")
    exit (1)

chan = int (argv [1])
chan = min (64, max (1, chan))

        
# Create K-meter processors. 
K20 = JackKmeter (chan, 'Meters')

# Allow Ctrl-C        
signal.signal (signal.SIGINT, signal.SIG_DFL)

# Set dark window background.
app = QtWidgets.QApplication(sys.argv)
pal = app.palette()
bgc = QtGui.QColor (40, 40, 40)
pal.setColor(pal.Window, bgc)
app.setPalette(pal)

# Create meter window.
metrics = makek20metrics (bgc)
win = Meterwin (chan, metrics)
sys.exit(app.exec_())


