FileDocCategorySizeDatePackage
SelectableAudioCapturePreview.javaAPI DocExample6660Wed Nov 10 12:55:22 GMT 2004com.oreilly.qtjnotebook.ch06

SelectableAudioCapturePreview

public class SelectableAudioCapturePreview extends Frame implements ItemListener

Fields Summary
static final Dimension
meterDim
Choice
deviceChoice
Checkbox
previewCheck
AudioLevelMeter
audioLevelMeter
SequenceGrabber
grabber
SGSoundChannel
soundChannel
SPBDevice
inputDriver
boolean
grabbing
Constructors Summary
public SelectableAudioCapturePreview()


        
        super ("Audio Preview");
        QTSessionCheck.check();
        setLayout (new GridLayout (4, 1));
        deviceChoice = new Choice();
        deviceChoice.addItemListener (this);
        add (deviceChoice);
        previewCheck = new Checkbox ("Preview", false);
        previewCheck.addItemListener (this);
        add (previewCheck);
        audioLevelMeter = new AudioLevelMeter();
        add (audioLevelMeter);
        // 4th row is reserved for later lab
        setUpAudioGrab();
        grabbing = true;
    
Methods Summary
public voiditemStateChanged(java.awt.event.ItemEvent e)

        try {
            if (e.getSource() == previewCheck) {
                if (previewCheck.getState())
                    soundChannel.setVolume (1.0f);
                else
                    soundChannel.setVolume (0.0f);
            } else if (e.getSource() == deviceChoice) {
                System.out.println ("changed device to "+
                                    deviceChoice.getSelectedItem());
                grabbing = false;
                // grabber.stop();
                soundChannel.setDevice (deviceChoice.getSelectedItem());
                // also reset inputDriver?
                inputDriver = soundChannel.getInputDriver();
                inputDriver.setLevelMeterOnOff (true);

                grabbing = true;
            }
        } catch (QTException qte) {
            qte.printStackTrace();
        }
    
public static voidmain(java.lang.String[] args)

        try {
            Frame f = new SelectableAudioCapturePreview();
            f.pack();
            f.setVisible(true);
        } catch (QTException qte) {
            qte.printStackTrace();
        }
    
protected voidsetUpAudioGrab()

        grabber = new SequenceGrabber();
        System.out.println ("got grabber");
        soundChannel = new SGSoundChannel (grabber);
        System.out.println ("Got SGAudioChannel");
        System.out.println ("SGChannelInfo = " +
                            soundChannel.getSoundInputParameters());
        System.out.println ("SoundDescription = " + 
                            soundChannel.getSoundDescription());

        // create list of input devices
        // getDeviceList flags: http://developer.apple.com/documentation/QuickTime/APIREF/SOURCESIII/sggetchanneldevicelist.htm
        // SGDeviceList devices = soundChannel.getDeviceList(StdQTConstants.sgDeviceListDontCheckAvailability);
        SGDeviceList devices = soundChannel.getDeviceList(0);
        int deviceCount = devices.getCount();
        for (int i=0; i<deviceCount; i++) {
            SGDeviceName deviceName = devices.getDeviceName(i);
            // is it available?
            if ((deviceName.getFlags() &
                 StdQTConstants.sgDeviceNameFlagDeviceUnavailable) == 0)
            deviceChoice.add(deviceName.getName());
        }

        // prepare and start previewing
        grabber.prepare(true,false);
        soundChannel.setUsage (StdQTConstants.seqGrabPreview);
        soundChannel.setVolume (0.0f);
        grabber.startPreview();

        inputDriver = soundChannel.getInputDriver();
        inputDriver.setLevelMeterOnOff (true);

        int[] levelTest = inputDriver.getActiveLevels();
        System.out.println (levelTest.length + " active levels");

        // set up thread to update level meter
        ActionListener timerCallback =
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (grabbing) {
                        try {
                            grabber.idle();
                            audioLevelMeter.repaint();
                        } catch (QTException qte) {
                            qte.printStackTrace();
                        }
                    }

                }
            };
        Timer timer = new Timer (50, timerCallback);
        timer.start();