FileDocCategorySizeDatePackage
AudioSampleBuilder.javaAPI DocExample7947Wed Nov 10 12:52:14 GMT 2004com.oreilly.qtjnotebook.ch07

AudioSampleBuilder

public class AudioSampleBuilder extends Object

Fields Summary
static final int
SAMPLING
static final byte[]
ONE_SECOND_SAMPLE
static final int
FREQUENCY
Constructors Summary
Methods Summary
public static QTHandlebuildOneSecondSample(int inTime)
Fill ONE_SECOND_SAMPLE with two-byte samples, according to some scheme (like square wave, sine wave, etc.) then wrap with QTHandle

        // convert inTime to sample count (ie, how many samples
        // past 0 we are)
        int wavelengthInSamples = SAMPLING / FREQUENCY;
        System.out.println ("wavelengthInSamples is " + 
                            wavelengthInSamples);
        int halfWavelength = wavelengthInSamples / 2;
        int sample = inTime * SAMPLING;
        for (int i=0; i<SAMPLING*2; i+=2) {
            int offset = sample % wavelengthInSamples;
            // square wave - bytes are either 7fff or 0000
            if (offset < halfWavelength) {
                ONE_SECOND_SAMPLE[i] = (byte) 0x7f;
                ONE_SECOND_SAMPLE[i+1] = (byte) 0xff;
            } else {
                ONE_SECOND_SAMPLE[i] = (byte) 0x00;
                ONE_SECOND_SAMPLE[i+1] = (byte) 0x00;
            }
            sample ++;
        }
        return new QTHandle (ONE_SECOND_SAMPLE);
    
public static voidmain(java.lang.String[] args)

 // A4 on piano
    // static final int FREQUENCY = 262; // middle C

          
        try {
            QTSessionCheck.check();

            QTFile movFile = new QTFile (new java.io.File("buildaudio.mov"));
            Movie movie =
                Movie.createMovieFile(movFile,
                                      StdQTConstants.kMoviePlayer,
                                      StdQTConstants.createMovieFileDeleteCurFile |
                                      StdQTConstants.createMovieFileDontCreateResFile);
            
            System.out.println ("Created Movie");
            
            // create an empty audio track
            int timeScale = SAMPLING; // 44100 units per second
            Track soundTrack = movie.addTrack (0, 0, 1);
                                               
            System.out.println ("Added empty Track");
            
            // create media for this track
            Media soundMedia = new SoundMedia (soundTrack,
                                               timeScale);
            System.out.println ("Created Media");

            // add samples
            soundMedia.beginEdits();
            
            // formats at
            // http://developer.apple.com/documentation/QuickTime/APIREF/SOURCESIV/soundformats.htm
            // "twos" is k16BitBigEndianFormat
            // int format = QTUtils.toOSType ("twos");
            int format = QTUtils.toOSType ("NONE");
            // int format = QTUtils.toOSType ("raw "); // oops, 8 bit
            
            SoundDescription soundDesc = new SoundDescription(format);
            System.out.println ("Created SoundDescription");

            soundDesc.setNumberOfChannels(1);
            soundDesc.setSampleSize(16);
            soundDesc.setSampleRate(SAMPLING);

            for (int i=0; i<5; i++) {
                // build the one-second sample
                QTHandle mediaHandle =  buildOneSecondSample (i);
                
                System.out.println ("mediaHandle length = " + 
                                    mediaHandle.getSize());
            
                soundMedia.addSample(mediaHandle, // QTHandleRef data,
                                     0, // int dataOffset,
                                     mediaHandle.getSize(), // int dataSize,
                                     1, // int durationPerSample,
                                     soundDesc, // SampleDescription sampleDesc,
                                     SAMPLING, // int numberOfSamples,
                                     0 // int sampleFlags)
                                     );
            }

            // finish editing and insert media into track
            soundMedia.endEdits();
            System.out.println ("Ended edits");
            soundTrack.insertMedia (0, // trackStart
                                    0, // mediaTime
                                    soundMedia.getDuration(), // mediaDuration
                                    1); // mediaRate
            System.out.println ("inserted media");

            // save up 
            System.out.println ("Saving...");
            OpenMovieFile omf = OpenMovieFile.asWrite (movFile);
            movie.addResource (omf,
                               StdQTConstants.movieInDataForkResID,
                               movFile.getName());
            System.out.println ("Done");

            System.exit(0);

        } catch (QTException qte) {
            qte.printStackTrace();
        }