mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-13 09:07:27 -05:00
Initial code check in continued
This commit is contained in:
85
src/source/mixer/ChannelShortAdapter.java
Normal file
85
src/source/mixer/ChannelShortAdapter.java
Normal file
@@ -0,0 +1,85 @@
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014 Dennis Sheirer
|
||||
*
|
||||
* 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/>
|
||||
******************************************************************************/
|
||||
package source.mixer;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
import source.Source.SampleType;
|
||||
|
||||
/**
|
||||
* Converts 16-bit little endian byte data into an array of floats
|
||||
*/
|
||||
public class ChannelShortAdapter extends SampleAdapter
|
||||
{
|
||||
private ByteOrder mByteOrder = ByteOrder.LITTLE_ENDIAN;
|
||||
private MixerChannel mMixerChannel;
|
||||
|
||||
public ChannelShortAdapter( MixerChannel channel )
|
||||
{
|
||||
/* Only use with LEFT/RIGHT channels */
|
||||
assert( channel != MixerChannel.MONO );
|
||||
|
||||
mMixerChannel = channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float[] convert( byte[] samples )
|
||||
{
|
||||
Float[] processed = new Float[ samples.length / 4 ];
|
||||
|
||||
int pointer = 0;
|
||||
|
||||
/* Wrap byte array in a byte buffer so we can process them as shorts */
|
||||
ByteBuffer buffer = ByteBuffer.wrap( samples );
|
||||
|
||||
/* Set endian to correct byte ordering */
|
||||
buffer.order( mByteOrder );
|
||||
|
||||
while( buffer.hasRemaining() )
|
||||
{
|
||||
if( mMixerChannel == MixerChannel.LEFT )
|
||||
{
|
||||
processed[ pointer ] = (float)buffer.getShort();
|
||||
|
||||
/* Throw away the right channel */
|
||||
buffer.getShort();
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Throw away the left channel */
|
||||
buffer.getShort();
|
||||
|
||||
processed[ pointer ] = (float)buffer.getShort();
|
||||
}
|
||||
|
||||
pointer++;
|
||||
}
|
||||
|
||||
return processed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set byte interpretation to little or big endian. Defaults to LITTLE
|
||||
* endian
|
||||
*/
|
||||
public void setByteOrder( ByteOrder order )
|
||||
{
|
||||
mByteOrder = order;
|
||||
}
|
||||
}
|
||||
23
src/source/mixer/MixerChannel.java
Normal file
23
src/source/mixer/MixerChannel.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014 Dennis Sheirer
|
||||
*
|
||||
* 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/>
|
||||
******************************************************************************/
|
||||
package source.mixer;
|
||||
|
||||
public enum MixerChannel
|
||||
{
|
||||
LEFT,RIGHT,MONO;
|
||||
}
|
||||
149
src/source/mixer/MixerEditor.java
Normal file
149
src/source/mixer/MixerEditor.java
Normal file
@@ -0,0 +1,149 @@
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014 Dennis Sheirer
|
||||
*
|
||||
* 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/>
|
||||
******************************************************************************/
|
||||
package source.mixer;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.sound.sampled.Mixer;
|
||||
import javax.swing.DefaultComboBoxModel;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JLabel;
|
||||
|
||||
import source.SourceEditor;
|
||||
import source.config.SourceConfigMixer;
|
||||
import source.config.SourceConfiguration;
|
||||
|
||||
public class MixerEditor extends SourceEditor
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
private JComboBox<String> mComboMixers;
|
||||
private JComboBox<MixerChannel>mComboChannels;
|
||||
protected Mixer.Info mSelectedMixer = null;
|
||||
|
||||
|
||||
public MixerEditor( SourceConfiguration config )
|
||||
{
|
||||
super( config );
|
||||
|
||||
initGUI();
|
||||
}
|
||||
|
||||
private void initGUI()
|
||||
{
|
||||
JLabel mixerLabel = new JLabel( "Mixer:" );
|
||||
add( mixerLabel, "align right" );
|
||||
|
||||
mComboMixers = new JComboBox<String>();
|
||||
|
||||
add( mComboMixers, "wrap" );
|
||||
|
||||
resetMixers();
|
||||
|
||||
JLabel channelLabel = new JLabel( "Channel:" );
|
||||
add( channelLabel, "align right" );
|
||||
|
||||
mComboChannels = new JComboBox<MixerChannel>();
|
||||
|
||||
add( mComboChannels, "wrap" );
|
||||
|
||||
resetChannels();
|
||||
}
|
||||
|
||||
public void reset()
|
||||
{
|
||||
resetMixers();
|
||||
resetChannels();
|
||||
}
|
||||
|
||||
public void save()
|
||||
{
|
||||
SourceConfigMixer config = (SourceConfigMixer)mConfig;
|
||||
|
||||
String mixer = mComboMixers.getItemAt( mComboMixers.getSelectedIndex() );
|
||||
|
||||
if( mixer != null )
|
||||
{
|
||||
config.setMixer( mixer );
|
||||
}
|
||||
|
||||
MixerChannel channel = mComboChannels.getItemAt(
|
||||
mComboChannels.getSelectedIndex() );
|
||||
|
||||
if( channel != null )
|
||||
{
|
||||
if( config.getChannel() != channel )
|
||||
{
|
||||
config.setChannel( channel );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void resetMixers()
|
||||
{
|
||||
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
Set<String> mixers = MixerManager.getInstance().getMixers();
|
||||
String[] mixerStrings = mixers.toArray( new String[ mixers.size() ] );
|
||||
|
||||
mComboMixers.setModel(
|
||||
new DefaultComboBoxModel<String>( mixerStrings ) );
|
||||
|
||||
SourceConfigMixer config = (SourceConfigMixer)mConfig;
|
||||
String mixer = config.getMixer();
|
||||
|
||||
if( mixer != null && mixers.contains( mixer ) )
|
||||
{
|
||||
mComboMixers.setSelectedItem( mixer );
|
||||
}
|
||||
else
|
||||
{
|
||||
//Mixer either hasn't been set yet, or it was set to a
|
||||
//mixer that is no longer available. Set the mixer to the
|
||||
//first item, and then store the value
|
||||
if( mixers.size() > 0 )
|
||||
{
|
||||
mComboMixers.setSelectedIndex( 0 );
|
||||
|
||||
config.setMixer( mComboMixers.getItemAt( 0 ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void resetChannels()
|
||||
{
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
mComboChannels.setModel(
|
||||
new DefaultComboBoxModel<MixerChannel>( MixerChannel.values() ) );
|
||||
|
||||
MixerChannel channel = ((SourceConfigMixer)mConfig).getChannel();
|
||||
|
||||
mComboChannels.setSelectedItem( channel );
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
258
src/source/mixer/MixerManager.java
Normal file
258
src/source/mixer/MixerManager.java
Normal file
@@ -0,0 +1,258 @@
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014 Dennis Sheirer
|
||||
*
|
||||
* 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/>
|
||||
******************************************************************************/
|
||||
package source.mixer;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.sound.sampled.AudioFormat;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.DataLine;
|
||||
import javax.sound.sampled.Line;
|
||||
import javax.sound.sampled.Mixer;
|
||||
import javax.sound.sampled.TargetDataLine;
|
||||
|
||||
import log.Log;
|
||||
import source.config.SourceConfigMixer;
|
||||
import source.config.SourceConfiguration;
|
||||
import source.tuner.MixerTunerDataLine;
|
||||
import source.tuner.MixerTunerType;
|
||||
import controller.channel.ProcessingChain;
|
||||
|
||||
public class MixerManager
|
||||
{
|
||||
private static MixerManager sInstance = null;
|
||||
|
||||
private HashMap<String,TargetDataLine> mMixerLines =
|
||||
new HashMap<String,TargetDataLine>();
|
||||
|
||||
private HashMap<String,MixerTunerDataLine> mMixerTuners =
|
||||
new HashMap<String,MixerTunerDataLine>();
|
||||
|
||||
public static AudioFormat MONO = new AudioFormat( 48000, //SampleRate
|
||||
16, //Sample Size
|
||||
1, //Channels
|
||||
true, //Signed
|
||||
false ); //Little Endian
|
||||
|
||||
public static AudioFormat STEREO = new AudioFormat( 48000, //SampleRate
|
||||
16, //Sample Size
|
||||
2, //Channels
|
||||
true, //Signed
|
||||
false ); //Little Endian
|
||||
|
||||
private MixerManager()
|
||||
{
|
||||
loadMixers();
|
||||
}
|
||||
|
||||
public static MixerManager getInstance()
|
||||
{
|
||||
if( sInstance == null )
|
||||
{
|
||||
sInstance = new MixerManager();
|
||||
}
|
||||
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
public MixerSource getSource( ProcessingChain channel )
|
||||
{
|
||||
MixerSource retVal = null;
|
||||
|
||||
SourceConfiguration config =
|
||||
channel.getChannel().getSourceConfiguration();
|
||||
|
||||
if( config instanceof SourceConfigMixer )
|
||||
{
|
||||
SourceConfigMixer mixerConfig = (SourceConfigMixer)config;
|
||||
|
||||
//Get the name of the requested mixer
|
||||
String mixerName = mixerConfig.getMixer();
|
||||
|
||||
if( mixerName != null )
|
||||
{
|
||||
TargetDataLine tdl = mMixerLines.get( mixerName );
|
||||
|
||||
if( tdl != null )
|
||||
{
|
||||
switch( mixerConfig.getChannel() )
|
||||
{
|
||||
case MONO:
|
||||
retVal = new MixerSource( mMixerLines.get( mixerName ),
|
||||
MONO,
|
||||
mixerName,
|
||||
new ShortAdapter() );
|
||||
break;
|
||||
case LEFT:
|
||||
case RIGHT:
|
||||
retVal = new MixerSource( mMixerLines.get( mixerName ),
|
||||
STEREO,
|
||||
mixerName,
|
||||
new ChannelShortAdapter( mixerConfig.getChannel() ) );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
public Set<String> getMixers()
|
||||
{
|
||||
return mMixerLines.keySet();
|
||||
}
|
||||
|
||||
public Collection<MixerTunerDataLine> getMixerTunerDataLines()
|
||||
{
|
||||
return mMixerTuners.values();
|
||||
}
|
||||
|
||||
private void loadMixers()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append( "MixerManager - loading system mixer devices\n" );
|
||||
|
||||
for( Mixer.Info mixerInfo: AudioSystem.getMixerInfo() )
|
||||
{
|
||||
//Sort between the mixers and the tuner mixers, and load each
|
||||
MixerTunerType mixerTunerType = MixerTunerType.getMixerTunerType( mixerInfo );
|
||||
|
||||
if( mixerTunerType == MixerTunerType.UNKNOWN )
|
||||
{
|
||||
Mixer mixer = AudioSystem.getMixer( mixerInfo );
|
||||
|
||||
if( mixer != null )
|
||||
{
|
||||
TargetDataLine tdl = getTargetDataLine( mixerInfo, STEREO );
|
||||
|
||||
if( tdl != null )
|
||||
{
|
||||
mMixerLines.put( mixerInfo.getName(), tdl );
|
||||
|
||||
sb.append( "\t[LOADED] Mixer:" + mixerInfo.getName() + "\n" );
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.append( "\t[NOT LOADED] Mixer:" + mixerInfo.getName() + " - audio format not supported\n" );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.append( "\t[NOT LOADED] Mixer:" + mixerInfo.getName() + " - couldn't get mixer\n" );
|
||||
}
|
||||
}
|
||||
else //Process as a Mixer-based tuner data line
|
||||
{
|
||||
TargetDataLine tdl =
|
||||
getTargetDataLine( mixerInfo,
|
||||
mixerTunerType.getAudioFormat() );
|
||||
|
||||
if( tdl != null )
|
||||
{
|
||||
switch( mixerTunerType )
|
||||
{
|
||||
case FUNCUBE_DONGLE_PRO:
|
||||
case FUNCUBE_DONGLE_PRO_PLUS:
|
||||
mMixerTuners.put( mixerInfo.getName(),
|
||||
new MixerTunerDataLine( tdl, mixerTunerType ) );
|
||||
sb.append( "\t[LOADED] FunCube Dongle Mixer Tuner:" + mixerInfo.getName() + "[" + mixerTunerType.getDisplayString() + "]\n" );
|
||||
break;
|
||||
case UNKNOWN:
|
||||
default:
|
||||
sb.append( "\t[NOT LOADED] Tuner:" + mixerInfo.getName() + " - unrecognized tuner type\n" );
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.append( "\t[NOT LOADED] Tuner:" + mixerInfo.getName() + " - couldn't get target data line\n" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Log.info( sb.toString() );
|
||||
}
|
||||
|
||||
private TargetDataLine getTargetDataLine( Mixer.Info mixerInfo, AudioFormat format )
|
||||
{
|
||||
TargetDataLine retVal = null;
|
||||
|
||||
Mixer mixer = AudioSystem.getMixer( mixerInfo );
|
||||
|
||||
if( mixer != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
DataLine.Info datalineInfo=
|
||||
new DataLine.Info(TargetDataLine.class, format );
|
||||
|
||||
retVal = (TargetDataLine) mixer.getLine( datalineInfo );
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
//Do nothing ... we couldn't get the TDL
|
||||
}
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
public static String getMixerDevices()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for( Mixer.Info mixerInfo: AudioSystem.getMixerInfo() )
|
||||
{
|
||||
sb.append( "\n--------------------------------------------------" );
|
||||
sb.append( "\nMIXER name:" + mixerInfo.getName() +
|
||||
"\n desc:" + mixerInfo.getDescription() +
|
||||
"\n vendor:" + mixerInfo.getVendor() +
|
||||
"\n version:" + mixerInfo.getVersion() +
|
||||
"\n" );
|
||||
|
||||
Mixer mixer = AudioSystem.getMixer( mixerInfo );
|
||||
|
||||
Line.Info[] sourceLines = mixer.getSourceLineInfo();
|
||||
|
||||
for( Line.Info lineInfo: sourceLines )
|
||||
{
|
||||
sb.append( " SOURCE LINE desc:" + lineInfo.toString() +
|
||||
"\n class:" + lineInfo.getClass() +
|
||||
"\n lineclass:" + lineInfo.getLineClass() +
|
||||
"\n" );
|
||||
}
|
||||
|
||||
Line.Info[] targetLines = mixer.getTargetLineInfo();
|
||||
|
||||
for( Line.Info lineInfo: targetLines )
|
||||
{
|
||||
sb.append( " TARGET LINE desc:" + lineInfo.toString() +
|
||||
"\n class:" + lineInfo.getClass() +
|
||||
"\n lineclass:" + lineInfo.getLineClass() +
|
||||
"\n" );
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
274
src/source/mixer/MixerSource.java
Normal file
274
src/source/mixer/MixerSource.java
Normal file
@@ -0,0 +1,274 @@
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014 Dennis Sheirer
|
||||
*
|
||||
* 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/>
|
||||
******************************************************************************/
|
||||
package source.mixer;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import javax.sound.sampled.AudioFormat;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.LineUnavailableException;
|
||||
import javax.sound.sampled.TargetDataLine;
|
||||
|
||||
import log.Log;
|
||||
import sample.Listener;
|
||||
import source.FloatArraySource;
|
||||
import source.SourceException;
|
||||
|
||||
public class MixerSource extends FloatArraySource
|
||||
{
|
||||
private int mFrequency = 0;
|
||||
private int mBufferSize = 16384;
|
||||
|
||||
private BufferReader mBufferReader = new BufferReader();
|
||||
private TargetDataLine mTargetDataLine;
|
||||
private AudioFormat mAudioFormat;
|
||||
private String mDisplayName;
|
||||
private int mBytesPerFrame = 0;
|
||||
private SampleAdapter mSampleAdapter;
|
||||
|
||||
CopyOnWriteArrayList<Listener<Float[]>> mSampleListeners =
|
||||
new CopyOnWriteArrayList<Listener<Float[]>>();
|
||||
|
||||
/**
|
||||
* Complex Mixer Source - constructs a reader on the mixer/sound card target
|
||||
* data line using the specified audio format (sample size, sample rate )
|
||||
* and broadcasts float[] sample data to all registered listeners. Reads
|
||||
* sample buffers sized to 10% of the sample rate specified in audio format.
|
||||
*
|
||||
* @param targetDataLine - mixer or sound card to be used
|
||||
*
|
||||
* @param format - audio format
|
||||
*
|
||||
* @param name - token name to use for this source
|
||||
*
|
||||
* @param sampleAdapter - adapter to convert byte array data read from the
|
||||
* mixer into float array data. Can optionally invert the channel data if
|
||||
* the left/right stereo channels are inverted.
|
||||
*/
|
||||
public MixerSource( TargetDataLine targetDataLine,
|
||||
AudioFormat format,
|
||||
String name,
|
||||
SampleAdapter sampleAdapter )
|
||||
{
|
||||
super( name );
|
||||
|
||||
mTargetDataLine = targetDataLine;
|
||||
mAudioFormat = format;
|
||||
mSampleAdapter = sampleAdapter;
|
||||
}
|
||||
|
||||
public void setListener( Listener<Float[]> listener )
|
||||
{
|
||||
mSampleListeners.add( listener );
|
||||
|
||||
/* If this is the first listener, start the reader thread */
|
||||
if( !mBufferReader.isRunning() )
|
||||
{
|
||||
Thread thread = new Thread( mBufferReader );
|
||||
thread.setName( getName() + " Sample Reader" );
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
|
||||
public void removeListener( Listener<Float[]> listener )
|
||||
{
|
||||
mSampleListeners.remove( listener );
|
||||
|
||||
/* If this is the laster listener, stop the reader thread */
|
||||
if( mSampleListeners.isEmpty() )
|
||||
{
|
||||
mBufferReader.stop();
|
||||
}
|
||||
}
|
||||
|
||||
public void broadcast( Float[] samples )
|
||||
{
|
||||
Iterator<Listener<Float[]>> it = mSampleListeners.iterator();
|
||||
|
||||
while( it.hasNext() )
|
||||
{
|
||||
Listener<Float[]> next = it.next();
|
||||
|
||||
/* if this is the last (or only) listener, send him the original
|
||||
* buffer, otherwise send him a copy of the buffer */
|
||||
if( it.hasNext() )
|
||||
{
|
||||
next.receive( Arrays.copyOf( samples, samples.length ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
next.receive( samples );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return mDisplayName;
|
||||
}
|
||||
|
||||
public int getSampleRate()
|
||||
{
|
||||
if( mTargetDataLine != null )
|
||||
{
|
||||
return (int)mTargetDataLine.getFormat().getSampleRate();
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the frequency of this source. Default is 0.
|
||||
*/
|
||||
public int getFrequency() throws SourceException
|
||||
{
|
||||
return mFrequency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the frequency that will be returned from this source. This may
|
||||
* be useful if you are streaming an external audio source in through the
|
||||
* sound card and you want to specify a frequency for that source
|
||||
*/
|
||||
public void setFrequency( int frequency )
|
||||
{
|
||||
mFrequency = frequency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reader thread. Performs blocking read against the mixer target data
|
||||
* line, converts the samples to an array of floats using the specified
|
||||
* adapter. Dispatches float arrays to all registered listeners.
|
||||
*/
|
||||
public class BufferReader implements Runnable
|
||||
{
|
||||
private AtomicBoolean mRunning = new AtomicBoolean();
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
if( mRunning.compareAndSet( false, true ) )
|
||||
{
|
||||
if( mTargetDataLine == null )
|
||||
{
|
||||
mRunning.set( false );
|
||||
|
||||
Log.error( "ComplexMixerSource - mixer target data line"
|
||||
+ " is null" );
|
||||
}
|
||||
else
|
||||
{
|
||||
mBytesPerFrame = mAudioFormat.getSampleSizeInBits() / 8;
|
||||
|
||||
if( mBytesPerFrame == AudioSystem.NOT_SPECIFIED )
|
||||
{
|
||||
mBytesPerFrame = 2;
|
||||
}
|
||||
|
||||
/* Set buffer size to 1/10 second of samples */
|
||||
mBufferSize = (int)( mAudioFormat.getSampleRate()
|
||||
* 0.05 ) * mBytesPerFrame;
|
||||
|
||||
/* We'll reuse the same buffer for each read */
|
||||
byte[] buffer = new byte[ mBufferSize ];
|
||||
|
||||
try
|
||||
{
|
||||
mTargetDataLine.open( mAudioFormat );
|
||||
|
||||
mTargetDataLine.start();
|
||||
}
|
||||
catch ( LineUnavailableException e )
|
||||
{
|
||||
Log.error( "ComplexMixerSource - mixer target data line"
|
||||
+ "not available to read data from - " +
|
||||
e.getLocalizedMessage() );
|
||||
|
||||
mRunning.set( false );
|
||||
}
|
||||
|
||||
while( mRunning.get() )
|
||||
{
|
||||
try
|
||||
{
|
||||
/* Blocking read - waits until the buffer fills */
|
||||
mTargetDataLine.read( buffer, 0, buffer.length );
|
||||
|
||||
/* Convert samples to float array */
|
||||
Float[] convertedSamples =
|
||||
mSampleAdapter.convert( buffer );
|
||||
|
||||
/* Dispatch samples to registered listeners */
|
||||
broadcast( convertedSamples );
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
Log.error( "ComplexMixerSource - error while reading"
|
||||
+ "from the mixer target data line - " +
|
||||
e.getLocalizedMessage() );
|
||||
|
||||
mRunning.set( false );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Close the data line if it is still open */
|
||||
if( mTargetDataLine != null && mTargetDataLine.isOpen() )
|
||||
{
|
||||
mTargetDataLine.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the reader thread
|
||||
*/
|
||||
public void stop()
|
||||
{
|
||||
if( mRunning.compareAndSet( true, false ) )
|
||||
{
|
||||
mTargetDataLine.stop();
|
||||
mTargetDataLine.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if the reader thread is running
|
||||
*/
|
||||
public boolean isRunning()
|
||||
{
|
||||
return mRunning.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose()
|
||||
{
|
||||
if( mBufferReader != null )
|
||||
{
|
||||
mBufferReader.stop();
|
||||
mSampleListeners.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
29
src/source/mixer/SampleAdapter.java
Normal file
29
src/source/mixer/SampleAdapter.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014 Dennis Sheirer
|
||||
*
|
||||
* 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/>
|
||||
******************************************************************************/
|
||||
package source.mixer;
|
||||
|
||||
import source.Source.SampleType;
|
||||
|
||||
/*
|
||||
* Adapter to convert byte data received from a data stream into float[]
|
||||
* samples
|
||||
*/
|
||||
public abstract class SampleAdapter
|
||||
{
|
||||
public abstract Float[] convert( byte[] samples );
|
||||
}
|
||||
63
src/source/mixer/ShortAdapter.java
Normal file
63
src/source/mixer/ShortAdapter.java
Normal file
@@ -0,0 +1,63 @@
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014 Dennis Sheirer
|
||||
*
|
||||
* 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/>
|
||||
******************************************************************************/
|
||||
package source.mixer;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
import source.Source.SampleType;
|
||||
|
||||
/**
|
||||
* Converts 16-bit little endian byte data into an array of floats
|
||||
*/
|
||||
public class ShortAdapter extends SampleAdapter
|
||||
{
|
||||
private ByteOrder mByteOrder = ByteOrder.LITTLE_ENDIAN;
|
||||
|
||||
@Override
|
||||
public Float[] convert( byte[] samples )
|
||||
{
|
||||
Float[] processed = new Float[ samples.length / 2 ];
|
||||
|
||||
int pointer = 0;
|
||||
|
||||
/* Wrap byte array in a byte buffer so we can process them as shorts */
|
||||
ByteBuffer buffer = ByteBuffer.wrap( samples );
|
||||
|
||||
/* Set endian to correct byte ordering */
|
||||
buffer.order( mByteOrder );
|
||||
|
||||
while( buffer.hasRemaining() )
|
||||
{
|
||||
processed[ pointer ] = (float)buffer.getShort();
|
||||
|
||||
pointer++;
|
||||
}
|
||||
|
||||
return processed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set byte interpretation to little or big endian. Defaults to LITTLE
|
||||
* endian
|
||||
*/
|
||||
public void setByteOrder( ByteOrder order )
|
||||
{
|
||||
mByteOrder = order;
|
||||
}
|
||||
}
|
||||
116
src/source/mixer/SystemMixerDevices.java
Normal file
116
src/source/mixer/SystemMixerDevices.java
Normal file
@@ -0,0 +1,116 @@
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014 Dennis Sheirer
|
||||
*
|
||||
* 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/>
|
||||
******************************************************************************/
|
||||
package source.mixer;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.sound.sampled.AudioFormat;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.DataLine;
|
||||
import javax.sound.sampled.Line;
|
||||
import javax.sound.sampled.LineUnavailableException;
|
||||
import javax.sound.sampled.Mixer;
|
||||
import javax.sound.sampled.TargetDataLine;
|
||||
|
||||
/**
|
||||
* THIS CLASS IS SCHEDULED FOR DELETION *****************************
|
||||
*/
|
||||
|
||||
public class SystemMixerDevices {
|
||||
|
||||
/**
|
||||
* Returns a hashmap of mixer names and target data lines that support the
|
||||
* audio format argument
|
||||
*
|
||||
* @param format - requested audio format
|
||||
* @return - mixer target data lines that support the requested audio format
|
||||
*/
|
||||
public static HashMap<String,TargetDataLine> getMixers( AudioFormat format )
|
||||
{
|
||||
HashMap<String,TargetDataLine> retVal = new HashMap<String,TargetDataLine>();
|
||||
|
||||
DataLine.Info datalineInfo=new DataLine.Info(TargetDataLine.class, format );
|
||||
|
||||
for( Mixer.Info mixerInfo: AudioSystem.getMixerInfo() )
|
||||
{
|
||||
Mixer mixer = AudioSystem.getMixer( mixerInfo );
|
||||
|
||||
if( mixer != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
TargetDataLine tdl = (TargetDataLine) mixer.getLine( datalineInfo );
|
||||
|
||||
if( tdl != null )
|
||||
{
|
||||
retVal.put( mixerInfo.getName(), tdl );
|
||||
}
|
||||
}
|
||||
catch( IllegalArgumentException iae )
|
||||
{
|
||||
//Do nothing ... mixer doesn't support the audio format
|
||||
}
|
||||
catch( LineUnavailableException e )
|
||||
{
|
||||
//Do nothing ... mixer doesn't support the audio format
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
public static String getSoundDevices()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for( Mixer.Info mixerInfo: AudioSystem.getMixerInfo() )
|
||||
{
|
||||
sb.append( "\n--------------------------------------------------" );
|
||||
sb.append( "\nMIXER name:" + mixerInfo.getName() +
|
||||
"\n desc:" + mixerInfo.getDescription() +
|
||||
"\n vendor:" + mixerInfo.getVendor() +
|
||||
"\n version:" + mixerInfo.getVersion() +
|
||||
"\n" );
|
||||
|
||||
Mixer mixer = AudioSystem.getMixer( mixerInfo );
|
||||
|
||||
Line.Info[] sourceLines = mixer.getSourceLineInfo();
|
||||
|
||||
for( Line.Info lineInfo: sourceLines )
|
||||
{
|
||||
sb.append( " SOURCE LINE desc:" + lineInfo.toString() +
|
||||
"\n class:" + lineInfo.getClass() +
|
||||
"\n lineclass:" + lineInfo.getLineClass() +
|
||||
"\n" );
|
||||
}
|
||||
|
||||
Line.Info[] targetLines = mixer.getTargetLineInfo();
|
||||
|
||||
for( Line.Info lineInfo: targetLines )
|
||||
{
|
||||
sb.append( " TARGET LINE desc:" + lineInfo.toString() +
|
||||
"\n class:" + lineInfo.getClass() +
|
||||
"\n lineclass:" + lineInfo.getLineClass() +
|
||||
"\n" );
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user