def __init__(self, *args):
        apply(Qwt.QwtPlot.__init__, (self,) + args)

        self.setTitle('Power spectrum');
        self.setCanvasBackground(QtCore.Qt.white)

        # grid
        self.grid = Qwt.QwtPlotGrid()
        self.grid.enableXMin(True)
        self.grid.setMajPen(QtGui.QPen(QtCore.Qt.gray, 0, QtCore.Qt.SolidLine))
        self.grid.attach(self)


        # axes
        self.setAxisTitle(Qwt.QwtPlot.xBottom, 'Frequency [Hz]')
        self.setAxisTitle(Qwt.QwtPlot.yLeft, 'Power [dB]')
        self.setAxisMaxMajor(Qwt.QwtPlot.xBottom, 10)
        self.setAxisMaxMinor(Qwt.QwtPlot.xBottom, 0)
        self.setAxisMaxMajor(Qwt.QwtPlot.yLeft, 10)
        self.setAxisMaxMinor(Qwt.QwtPlot.yLeft, 0)
        self.setAxisScaleEngine(Qwt.QwtPlot.xBottom, Qwt.QwtLog10ScaleEngine())
        self.setAxisScale(Qwt.QwtPlot.xBottom, 50.0, 22050/2)
        self.setAxisScale(Qwt.QwtPlot.yLeft, -40.0, 30.0)

        # curves
        PENWIDTH = 1

        self.curve1 = Qwt.QwtPlotCurve('PSTrace1')
        self.curve1.setPen(QtGui.QPen(QtCore.Qt.blue, PENWIDTH))
        self.curve1.setYAxis(Qwt.QwtPlot.yLeft)
        self.curve1.attach(self)


        self.dt=1.0/1600
        self.df=1.0/(512*self.dt)
        self.f = np.arange(0.0, 16000, self.df)
        self.a = 0.0*self.f
        self.p = 0.0*self.f
        self.curve1.setData(self.f, self.a)

        self.fs = None
        self.data = None
        self.cur_idx = None
        self.fft_analyzer = None
        self.timer_id = None

        # stream
        self.audio_input_stream = None
        self.audio_input_buffer = None

        self.fft_analyzer = FFTAnalyzer(fs=22050)
        self.replot()
class PowerSpectrumScope(Qwt.QwtPlot):
    """
    Power spectrum display widget
    """
    def __init__(self, *args):
        apply(Qwt.QwtPlot.__init__, (self,) + args)

        self.setTitle('Power spectrum');
        self.setCanvasBackground(QtCore.Qt.white)

        # grid
        self.grid = Qwt.QwtPlotGrid()
        self.grid.enableXMin(True)
        self.grid.setMajPen(QtGui.QPen(QtCore.Qt.gray, 0, QtCore.Qt.SolidLine))
        self.grid.attach(self)


        # axes
        self.setAxisTitle(Qwt.QwtPlot.xBottom, 'Frequency [Hz]')
        self.setAxisTitle(Qwt.QwtPlot.yLeft, 'Power [dB]')
        self.setAxisMaxMajor(Qwt.QwtPlot.xBottom, 10)
        self.setAxisMaxMinor(Qwt.QwtPlot.xBottom, 0)
        self.setAxisMaxMajor(Qwt.QwtPlot.yLeft, 10)
        self.setAxisMaxMinor(Qwt.QwtPlot.yLeft, 0)
        self.setAxisScaleEngine(Qwt.QwtPlot.xBottom, Qwt.QwtLog10ScaleEngine())
        self.setAxisScale(Qwt.QwtPlot.xBottom, 50.0, 22050/2)
        self.setAxisScale(Qwt.QwtPlot.yLeft, -40.0, 30.0)

        # curves
        PENWIDTH = 1

        self.curve1 = Qwt.QwtPlotCurve('PSTrace1')
        self.curve1.setPen(QtGui.QPen(QtCore.Qt.blue, PENWIDTH))
        self.curve1.setYAxis(Qwt.QwtPlot.yLeft)
        self.curve1.attach(self)


        self.dt=1.0/1600
        self.df=1.0/(512*self.dt)
        self.f = np.arange(0.0, 16000, self.df)
        self.a = 0.0*self.f
        self.p = 0.0*self.f
        self.curve1.setData(self.f, self.a)

        self.fs = None
        self.data = None
        self.cur_idx = None
        self.fft_analyzer = None
        self.timer_id = None

        # stream
        self.audio_input_stream = None
        self.audio_input_buffer = None

        self.fft_analyzer = FFTAnalyzer(fs=22050)
        self.replot()

    def timerEvent(self,e):     # FFT
        ## file
        #cur_data = self.data[self.cur_idx: self.cur_idx+self.fft_analyzer.frame_size]
        #amp, angle = self.fft_analyzer.calc_spectrum(cur_data)
        #y = np.arange(self.fft_analyzer.frame_size/2.)/self.fft_analyzer.frame_size*self.fft_analyzer.fs
        #
        #self.curve1.setData(y, amp)
        #self.replot()
        #self.cur_idx += self.fft_analyzer.shift
        #if self.cur_idx > len(self.data) - self.fft_analyzer.frame_size:
        #    self.killTimer(self.timer_id)
        #    self.timer_id = None

        # stream
        #self.audio_input_stream.start(self.audio_input_buffer)
        data = self.audio_input_buffer.data()
        data = np.frombuffer(data, dtype=np.int16)
        amp, angle = self.fft_analyzer.calc_spectrum(data[self.cur_idx: self.cur_idx+self.fft_analyzer.frame_size]/(2.**15))
        y = np.arange(self.fft_analyzer.frame_size/2.)/self.fft_analyzer.frame_size*self.fft_analyzer.fs

        self.curve1.setData(y, amp)
        self.replot()
        self.cur_idx += 0.08*self.fft_analyzer.fs
        print data.size

    # todo: そのうち実装
    def state_changed(self, new_state):
        if new_state == QtMultimedia.QAudio.StoppedState:
            if self.audio_input_stream.error() != QtMultimedia.QAudio.NoError:
                print 'state_changed: error'
            else:
                print 'fin rec'
                print len(self.audio_input_buffer.data())
        elif new_state == QtMultimedia.QAudio.ActiveState:
            pass


    def startPlot(self):
        if self.timer_id is None:
            self.timer_id = self.startTimer(80)
        else:
            raise Exception('Already started plotting.')

        audio_device_info = QtMultimedia.QAudioDeviceInfo()
        ua1g = audio_device_info.defaultInputDevice()

        audio_format = QtMultimedia.QAudioFormat()
        audio_format.setByteOrder(QtMultimedia.QAudioFormat.LittleEndian)
        audio_format.setChannelCount(1)
        audio_format.setCodec('audio/pcm')
        audio_format.setFrequency(22050)
        audio_format.setSampleSize(16)
        audio_format.setSampleType(QtMultimedia.QAudioFormat.SignedInt)
        print audio_format

        print 'is supported?: ', ua1g.isFormatSupported(audio_format)

        self.audio_input_buffer = QtCore.QBuffer()
        self.audio_input_buffer.open(QtCore.QBuffer.ReadWrite)

        self.audio_input_stream = QtMultimedia.QAudioInput(ua1g, audio_format)
        self.audio_input_stream.stateChanged.connect(self.state_changed)
        self.audio_input_stream.setBufferSize(256)
        self.audio_input_stream.start(self.audio_input_buffer)
        self.cur_idx = 0


    def stopPlot(self):
        if self.timer_id is not None:
            self.killTimer(self.timer_id)
            self.timer_id = None
        else:
            raise Exception('Not started plotting.')

        self.audio_input_buffer = None
        self.audio_input_stream = None