/*
 * Copyright and license
 * Copyright (c) 2009 University of Helsinki
 *
 *	Permission is hereby granted, free of charge, to any person
 *	obtaining a copy of this software and associated documentation
 *	files (the "Software"), to deal in the Software without
 *	restriction, including without limitation the rights to use,
 *	copy, modify, merge, publish, distribute, sublicense, and/or 
 *	sell copies of the Software, and to permit persons to whom the
 *	Software is furnished to do so, subject to the following
 *	conditions:
 *
 *	The above copyright notice and this permission notice shall be
 *	included in all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * 	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *	OTHER DEALINGS IN THE SOFTWARE.
 *
 *  more information:
 *	http://www.opensource.org/licenses/mit-license.php
 */

package examples;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import fi.helsinki.cs.ohtu.mpeg2.audio.mpa.MP2Data;
import fi.helsinki.cs.ohtu.mpeg2.audio.mpa.QuantizationTables;
import fi.helsinki.cs.ohtu.mpeg2.util.BitOutputStream;

/**
 * Writes an MP2 stream with some sound.
 * 
 * @author Pakkaamo
 */
public class WriteDummyMP2Stream {
	
    public static void main(String[] args) throws IOException {
    	
        int[] bitAlloc = new int[32];
        int[] scfsi    = new int[32];
        int[][] scales = new int[32][3];
        int[][][] samples = new int[32][3][12];
        int[] noiseBand = { 2800, 0, 1400, 0,
                            2800, 0, 1400, 0,
                            2800, 0, 1400, 0 };
        int[] silentBand0 = { 7, 7, 7, 7,
                            7, 7, 7, 7,
                            7, 7, 7, 7 };
        int[] silentBand1 = { 1, 1, 1, 1,
                            1, 1, 1, 1,
                            1, 1, 1, 1 };
        FileOutputStream fout;
        BitOutputStream bout;
        MP2Data ad;
        MP2Data.Channel ch;

        if (args.length != 1) {
            System.err.println("no output file given");
            System.exit(1);
        }

        // Careful now... A frame must be 96 bytes long.
        bitAlloc[0] = 3;
        bitAlloc[1] = 13;
        bitAlloc[2] = 1;
        samples[0][0] = samples[0][1] = samples[0][2] = silentBand0;
        samples[1][0] = samples[1][1] = samples[1][2] = noiseBand;
        samples[2][0] = samples[2][1] = samples[2][2] = silentBand1;

        ch = new MP2Data.Channel(bitAlloc, scfsi, scales, samples);
        ad = new MP2Data(ch, QuantizationTables.getTable(48000, 32));

        fout = new FileOutputStream(new File(args[0]));
        bout = new BitOutputStream(fout,
                BitOutputStream.BitOrder.MOST_SIGNIFICANT_FIRST);

        for (int i = 0; i < 100; i++) {
            // Write a few frames so that the sound can be heard. Hope
            // nobody will freak out...
            bout.writeLowBits(0xfffd14c0L, 32); // Header: v1, layer II,
                                                //    no CRC, 32kbit/s,
                                                //    48kHz, no padding, mono
            ad.writeTo(bout);
            bout.byteAlign();
        }
        bout.close();
    }
}