FileDocCategorySizeDatePackage
AudioCapturePreview.javaAPI DocExample5337Wed Nov 10 12:39:08 GMT 2004com.oreilly.qtjnotebook.ch06

AudioCapturePreview

public class AudioCapturePreview extends Frame implements ItemListener

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


        
        super ("Audio Preview");
        QTSessionCheck.check();
        setLayout (new GridLayout (3, 1));
        add (new Panel()); // reserved for next lab
        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);
            }
        } catch (QTException qte) {
            qte.printStackTrace();
        }
    
public static voidmain(java.lang.String[] args)

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

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

        // 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) {
                    // audioLevelMeter.repaint();
                    if (grabbing) {
                        try {
                            grabber.idle();
                            audioLevelMeter.repaint();
                        } catch (QTException qte) {
                            qte.printStackTrace();
                        }
                    }

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