mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-10 23:57:25 -05:00
Redesigned audio manager handling of audio outputs to ensure that only one channel's audio can be routed to an audio output at a time. Changed audio outputs to use the shared thread pool manager for scheduled tasks, instead of creating a thread pool for each audio output.
136 lines
3.1 KiB
Java
136 lines
3.1 KiB
Java
package audio.output;
|
|
|
|
import java.util.concurrent.LinkedTransferQueue;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
import javax.sound.sampled.FloatControl;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import sample.Broadcaster;
|
|
import sample.Listener;
|
|
import audio.AudioEvent;
|
|
import audio.AudioEvent.Type;
|
|
import audio.AudioPacket;
|
|
import audio.metadata.AudioMetadata;
|
|
import controller.ThreadPoolManager;
|
|
|
|
public abstract class AudioOutput implements Listener<AudioPacket>
|
|
{
|
|
private final static Logger mLog = LoggerFactory.getLogger( AudioOutput.class );
|
|
|
|
private AtomicBoolean mSquelched = new AtomicBoolean( false );
|
|
private Listener<AudioMetadata> mAudioMetadataListener;
|
|
|
|
protected Broadcaster<AudioEvent> mAudioEventBroadcaster = new Broadcaster<>();
|
|
protected LinkedTransferQueue<AudioPacket> mBuffer = new LinkedTransferQueue<>();
|
|
protected ThreadPoolManager mThreadPoolManager;
|
|
protected boolean mCanProcessAudio;
|
|
protected long mLastActivity;
|
|
|
|
public AudioOutput( ThreadPoolManager threadPoolManager )
|
|
{
|
|
mThreadPoolManager = threadPoolManager;
|
|
}
|
|
|
|
public void reset()
|
|
{
|
|
broadcast( new AudioEvent( AudioEvent.Type.AUDIO_STOPPED,
|
|
getChannelName() ) );
|
|
}
|
|
|
|
public void dispose()
|
|
{
|
|
mCanProcessAudio = false;
|
|
|
|
mBuffer.clear();
|
|
|
|
mAudioEventBroadcaster.dispose();
|
|
mAudioEventBroadcaster = null;
|
|
mAudioMetadataListener = null;
|
|
|
|
mThreadPoolManager = null;
|
|
}
|
|
|
|
public abstract String getChannelName();
|
|
|
|
public abstract void setMuted( boolean muted );
|
|
public abstract boolean isMuted();
|
|
|
|
public abstract FloatControl getGainControl();
|
|
public abstract boolean hasGainControl();
|
|
|
|
protected void broadcast( AudioMetadata metadata )
|
|
{
|
|
if( mAudioMetadataListener != null )
|
|
{
|
|
mAudioMetadataListener.receive( metadata );
|
|
}
|
|
}
|
|
|
|
protected void broadcast( AudioEvent audioEvent )
|
|
{
|
|
mAudioEventBroadcaster.broadcast( audioEvent );
|
|
}
|
|
|
|
public void setSquelched( boolean squelched )
|
|
{
|
|
mSquelched.set( squelched );
|
|
|
|
broadcast( new AudioEvent( squelched ? Type.AUDIO_SQUELCHED :
|
|
Type.AUDIO_UNSQUELCHED, getChannelName() ) );
|
|
}
|
|
|
|
public boolean isSquelched()
|
|
{
|
|
return mSquelched.get();
|
|
}
|
|
|
|
/**
|
|
* Registers a single listener to receive audio start and audio stop events
|
|
*/
|
|
public void addAudioEventListener( Listener<AudioEvent> listener )
|
|
{
|
|
mAudioEventBroadcaster.addListener( listener );
|
|
}
|
|
|
|
public void removeAudioEventListener( Listener<AudioEvent> listener )
|
|
{
|
|
mAudioEventBroadcaster.removeListener( listener );
|
|
}
|
|
|
|
/**
|
|
* Registers a single listener to receive the audio metadata from each
|
|
* audio packet
|
|
*/
|
|
public void setAudioMetadataListener( Listener<AudioMetadata> listener )
|
|
{
|
|
mAudioMetadataListener = listener;
|
|
}
|
|
|
|
public void removeAudioMetadataListener()
|
|
{
|
|
mAudioMetadataListener = null;
|
|
}
|
|
|
|
/**
|
|
* Timestamp when most recent buffer was received
|
|
*/
|
|
public long getLastBufferReceived()
|
|
{
|
|
return mLastActivity;
|
|
}
|
|
|
|
@Override
|
|
public void receive( AudioPacket packet )
|
|
{
|
|
if( mCanProcessAudio )
|
|
{
|
|
mLastActivity = System.currentTimeMillis();
|
|
|
|
mBuffer.add( packet );
|
|
}
|
|
}
|
|
}
|