/*
  Copyright (C) 2010-2011  Charles Pence <charles@charlespence.net>
  Copyright (C) 1996-1997  Id Software, Inc.

  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/>.
*/
#ifndef SOUND_CHANNEL_H__
#define SOUND_CHANNEL_H__

namespace sound
{

class Sample;


class Channel
{
public:
    Channel();
    
    // Allocate this channel before playing
    void Allocate();
    
    // Calculate left and right volume based on listener origin and right
    void Spatialize(vec3_t listener_origin, vec3_t listener_right);
    
    // Play it
    void Play()
    {
        if (!sample_)
            return;
        Mix_PlayChannel(mix_channel_, sample_->chunk(), looping_? -1 : 0);
        started_ = SDL_GetTicks();
    }
    
    inline bool playing() const { return Mix_Playing(mix_channel_) != 0; }
    inline void stop() { sample_.reset(); Mix_HaltChannel(mix_channel_); }
    
    Sample::Pointer sample_;
    int mix_channel_;
    bool looping_;
    
    int started_;
    
    byte left_vol_;
    byte right_vol_;
    float master_vol_;
    
    int entity_num_;
    int entity_channel_;
    
    vec3_t origin_;
    float dist_mult_;
};

}

#endif /* SOUND_CHANNEL_H__ */
