示例#1
0
SoundSource::SoundSource(const SoundSource& copy)
: m_channel(copy.m_channel)
{
    setPitch(copy.getPitch());
    setVolume(copy.getVolume());
    setPosition(copy.getPosition());
    setRelativeToListener(copy.isRelativeToListener());
    setMinDistance(copy.getMinDistance());
    setAttenuation(copy.getAttenuation());
}
示例#2
0
SoundSource::SoundSource(const SoundSource& copy)
{
    alCheck(alGenSources(1, &m_source));
    alCheck(alSourcei(m_source, AL_BUFFER, 0));

    setPitch(copy.getPitch());
    setVolume(copy.getVolume());
    setPosition(copy.getPosition());
    setRelativeToListener(copy.isRelativeToListener());
    setMinDistance(copy.getMinDistance());
    setAttenuation(copy.getAttenuation());
}
示例#3
0
void DeviceAudioDX11::tick (const DTfloat dt)
{		

	PROFILER(PROFILER_SOUND);
    
    if (!_x_audio_2)
        return;

    DTfloat sound_on = castFromString<DTboolean>(Globals::getGlobal("SYS_SOUND")) ? 1.0F : 0.0F;		
    DTfloat sound_gain = Globals::hasGlobal("SYS_SOUND_GAIN") ? castFromString<DTfloat>(Globals::getGlobal("SYS_SOUND_GAIN")) : 1.0F;

    _gain = sound_on * sound_gain;
    
    //
    // Update Camera
    //

    if (getCamera()) {
        // Update Listener
        Vector3 position = getCamera()->getTranslation();
        Vector3 velocity = getCamera()->getVelocity();
        
        Vector3 forward = getCamera()->getForwards();
        Vector3 up = getCamera()->getUp();

        _x3_listener.OrientFront = X3DAUDIO_VECTOR(forward.x,forward.y,forward.z);

        _x3_listener.OrientTop = X3DAUDIO_VECTOR(up.x,up.y,up.z);

        _x3_listener.Position = X3DAUDIO_VECTOR(position.x,position.y,position.z);

        _x3_listener.Velocity = X3DAUDIO_VECTOR(velocity.x,velocity.y,velocity.z);
        
        _x_master_voice->SetVolume(_gain);
    } else {
        _x3_listener.OrientFront = X3DAUDIO_VECTOR(0.0F,0.0F,-1.0F);

        _x3_listener.OrientTop = X3DAUDIO_VECTOR(0.0F,1.0F,0.0F);

        _x3_listener.Position = X3DAUDIO_VECTOR(0.0F,0.0F,0.0F);

        _x3_listener.Velocity = X3DAUDIO_VECTOR(0.0F,0.0F,0.0F);
        
        _x_master_voice->SetVolume(_gain);
    }


    //
    // Update channels
    //

    for (DTuint c = 0; c < _channels.size(); ++c) {
        DeviceAudioDX11Channel &channel = _channels[c];
        SoundSource *source = channel._source;
    
        // If there's a source and a sound then this channel is playing
        if (source) {
            SoundPacket &packet = channel._packets[channel._current_packet];

            // Check to see if sound buffer was starved
            if (!channel._ready_to_start && packet.getNumBytes() > 0 && !channel._is_playing) {
                
                channel._ready_to_start = true;
                LOG_MESSAGE << "Audio stream starved";
            
            // Check if sound data has been depleted
            } else if (!channel._ready_to_start && packet.getNumBytes() == 0 && !channel._is_playing) {
#if DT2_MULTITHREADED_AUDIO
                AutoSpinLockRecursive lock(&_lock);
#endif

                stopOnChannel(channel);
                LOG_MESSAGE << "Audio stream done";
                
            // Update sound
            } else {
                // Maybe channel needs to be started
                if (channel._ready_to_start) {
#if DT2_MULTITHREADED_AUDIO
                    AutoSpinLockRecursive lock(&_lock);
#endif

                    // Fill buffers
                    if (channel._needs_priming)
                        primeStreaming (channel);

                    channel._is_playing = true;
                    channel._x_voice->Start();
              
                    channel._ready_to_start = false;
                }
                
                Vector3 position = source->getTranslation();
                Vector3 velocity = source->getVelocity();
                //DTboolean looping = source->Get_Looping();	// Instead we loop sounds ourselves
                DTfloat pitch = source->getPitch();
                DTfloat rolloff = source->getRolloff();
                DTfloat gain = source->getGain();

                // position sound
                if (!getCamera()) {
                    channel._x3_emitter.Position = X3DAUDIO_VECTOR(0.0F,0.0F,0.0F);

                } else if (position == Vector3(0.0F,0.0F,0.0F)) {
                    Vector3 camera_position = getCamera()->getTranslation();
                    channel._x3_emitter.Position = X3DAUDIO_VECTOR(camera_position.x, camera_position.y, camera_position.z);

                } else {
                    channel._x3_emitter.Position = X3DAUDIO_VECTOR(position.x, position.y, position.z);

                }

                channel._x3_emitter.Velocity = X3DAUDIO_VECTOR(velocity.x, velocity.y, velocity.z);
                channel._x_voice->SetVolume(gain);

                //::alSourcef(channel._al_source, AL_PITCH, pitch);
                //CheckAL();

                //::alSourcef(channel._al_source, AL_ROLLOFF_FACTOR, rolloff);
                //CheckAL();
                
            }
        }
    }
    
    // Update Busses
    List<Bus>::iterator i;
    FOR_EACH (i,_busses) {
        Bus &bus = *i;
    
        // Get the number of buffers that have been processed
        //ALint num_buffers;
        //::alGetSourcei(bus._al_source, AL_BUFFERS_PROCESSED, &num_buffers);
        //bus._num_active -= num_buffers;
            
        // Pump all of the buffers
        //while (bus._num_active < 3) {
        //
        //    // Deque one buffer
        //    //ALuint buffer;
        //    //while (num_buffers > 0) {
        //    //    ::alSourceUnqueueBuffers(bus._al_source, 1, &buffer );
        //    //    --num_buffers;
        //    //    CheckAL();
        //    //}
        //    
        //    // Stream chunk in	
        //    SoundPacket packet = bus._source->getNextSoundPacket();
        //    if (packet.getNumBytes() == 0)
        //        break;

        //    // Choose appropriate format
        //    //ALenum format;
        //    //switch(packet.getFormat()) {
        //    //    case SoundResource::FORMAT_MONO8:		format = AL_FORMAT_MONO8;		break;
        //    //    case SoundResource::FORMAT_MONO16:		format = AL_FORMAT_MONO16;		break;
        //    //    case SoundResource::FORMAT_STEREO8:		format = AL_FORMAT_STEREO8;		break;
        //    //    case SoundResource::FORMAT_STEREO16:	format = AL_FORMAT_STEREO16;	break;
        //    //};
        //        
        //    // Store data into the buffer
        //    //CheckAL();
        //    //::alBufferData(bus._buffer[0], format, packet.getBuffer(), packet.getNumBytes(), packet.getFrequency());
        //    //CheckAL();
        //    
        //    // Enqueue the buffer
        //    //::alSourceQueueBuffers(bus._al_source, 1, &(bus._buffer[0]));
        //    
        //    // Rotate the ring buffer
        //    //ALuint temp = bus._buffer[0];
        //    //bus._buffer[0] = bus._buffer[1];
        //    //bus._buffer[1] = bus._buffer[2];
        //    //bus._buffer[2] = temp;

        //    ++bus._num_active;
        //}
        
        if (bus._num_active > 0) {
            // Check to make sure source is playing if there is new data
            //ALenum state;
            //::alGetSourcei(bus._al_source, AL_SOURCE_STATE, &state);
            //CheckAL();
            //    
            //// Check to see if sound buffer was starved
            //if (state != AL_PLAYING) {
            //    ::alSourcePlay(bus._al_source);
            //}
        }
    }