/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *
 * This file is part of NMM. 
 * 
 * NMM 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; only version 2 of the License.      
 * 
 * NMM 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 NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef _NMM_SCOPE_NODE_HPP_
#define _NMM_SCOPE_NODE_HPP_

#include "nmm/multimedia/GenericProcessorNode.hpp"

#include "nmm/multimedia/ISyncResetImpl.hpp"
#include "IScopeImpl.hpp"

namespace NMM {

  //! Oszilloscope Node.
  class ScopeNode :
    public GenericProcessorNode,
    public IScopeImpl,
    public ISyncResetImpl
  {
  private:
    // Input parameters
    int input_channels;    //!< The number of channels in the audio input.
    int sample_rate;       //!< The audio input sample rate.
    int byte_per_sample;
    

    // Output parameters
    int image_height;      //!< The image width and height chosen with the
    int image_width;       //!< output format.
    int range;             //!< The resulting array size for an image.
    double framerate;

    // Internally used variables
    Buffer* old_in_buffer; //!< The last dequeued in_buffer.
    int curr_audio;        //!< The current position in the audio buffer.
    int curr_video;        //!< The current column position in the video
                           //!< frame.
    int gap;               //!< The number of audio bytes to leave out between
                           //!< two frames.
    int audio_end;         //!< The number of bytes in the actual audio buffer.
    char* audio;           //!< The audio data.
    
    unsigned char* image;  //!< Used to compute the next image; it is 
                           //!< necessary to store the image because of
                           //!< the blur effect.

    bool blur_enabled;     //!< True iff the blur effect is enabled
    int blur_step;         //!< The value to subtract from the last frame
                           //!< for blur effect.
    
    Timestamp valid_ts;    //!< Last valid timestamp
    Timestamp in_ts;       //!< Timestamp of current input buffer
    Timestamp out_ts;      //!< Timestamp of next output buffer
    int valid_samples;     //!< Audio samples since last valid timestamp
    int in_samples;        //!< Audio samples since    
    
    //! Sets a point at position (x, y) in the image buffer.
    void setPoint(unsigned char* image, int x, int y);
    
  protected:
    /**
     * Reset synchronization.
     */
    Result syncReset();
    
    /**
     * Enables or disables the blur effect.
     */
    Result setBlurEnabled(const nmm_bool& toggle);
    
    /**
     * sets the intensity of the blur effect.
     */
    Result setBlurStep(const nmm_int32& step);
    
    //! The doInit - method from Node.
    Result doInit();
    
    //! The doDeinit - method from Node.
    Result doDeinit();
    
    //! The doInitOutput - method from Node.
    Result doInitOutput();
    
    //! The doDeinitOutput - method from Node.
    Result doDeinitOutput();
    
    //! The doActivate - method from Node.
    Result doActivate();
    
    //! The doDeactivate - method from Node.
    Result doDeactivate();
    
    //! The processBuffer - method from GenericNode.
    Buffer* processBuffer(Buffer* in_buffer);
    
  public:
    //! The main constructor.
    ScopeNode(const char *name = "Scope Node",
	      const StreamQueue::Mode queue_mode = StreamQueue::DEFAULT_MODE,
	      const int size = StreamQueue::DEFAULT_SIZE);

    //! The main destructor.
    virtual ~ScopeNode();
  };

}

#endif // _NMM_SCOPE_NODE_HPP_


