<commit_before>/*
 * Copyright 1999-2002 Carnegie Mellon University.  
 * Portions Copyright 2002 Sun Microsystems, Inc.  
 * Portions Copyright 2002 Mitsubishi Electronic Research Laboratories.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 *
 */


package tests.frontend;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

import java.net.URL;

import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;

import edu.cmu.sphinx.frontend.AudioSource;
import edu.cmu.sphinx.frontend.FrontEnd;
import edu.cmu.sphinx.frontend.Feature;
import edu.cmu.sphinx.frontend.FeatureExtractor;
import edu.cmu.sphinx.frontend.FeatureFrame;
import edu.cmu.sphinx.frontend.StreamAudioSource;

import edu.cmu.sphinx.util.SphinxProperties;


/**
 * This program takes in an audio file, does frontend signal processing to it,
 * and then dumps the resulting Cepstra into a separate binary file.
 *
 * This program takes three arguments:
 *
 * 1. propsFile - the Sphinx properties file
 * 2. audioFile - the name of the audio file
 * 3. outputFile - the name of the binary output file
 */
public class CepstrumFileDumper {

    private FrontEnd frontend;
    private List allFeatures;
    private int featureLength;
    
    /**
     * Constructs a CepstrumFileDumper.
     *
     * @param propsFile the Sphinx properties file
     * @param inputAudioFile the input audio file
     */
    public CepstrumFileDumper(String propsFile, String inputAudioFile)
	throws FileNotFoundException, IOException {
        try {
	    String context = "cepstralDumping";
	    String pwd = System.getProperty("user.dir");
	    SphinxProperties.initContext
		(context, new URL("file://" + pwd + File.separatorChar +
				  propsFile));
	    SphinxProperties props = SphinxProperties.getSphinxProperties
		(context);
	    featureLength = props.getInt
		(FeatureExtractor.PROP_FEATURE_LENGTH, 13);

	    AudioSource audioSource = new StreamAudioSource
		("BatchFileAudioSource", context,
		 (new FileInputStream(inputAudioFile)), inputAudioFile);

	    frontend = new FrontEnd("frontend", context, audioSource);

	    allFeatures = new LinkedList();
	    getAllFeatures();

	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * Retrieve all Features from the frontend, and cache all those
     * with actual feature data.
     */
    private void getAllFeatures() throws IOException {
	Feature feature = null;
	FeatureFrame frame = null;
	while ((frame = frontend.getFeatureFrame(1)) != null) {
	    feature = frame.getFeatures()[0];
	    if (feature.hasContent()) {
		allFeatures.add(feature);
	    }
	}
    }

    /**
     * Returns the total number of data points that should be written
     * to the output file.
     *
     * @return the total number of data points that should be written
     */
    private int getNumberDataPoints() {
	return (allFeatures.size() * featureLength);
    }

    /**
     * Dumps the Cepstra to the given binary output.
     *
     * @param outputFile the binary output file
     */
    public void dumpBinary(String outputFile) throws IOException {
	DataOutputStream outStream = new DataOutputStream
	    (new FileOutputStream(outputFile));
	outStream.writeFloat((float) getNumberDataPoints());

	for (Iterator i = allFeatures.iterator(); i.hasNext();) {
	    Feature feature = (Feature) i.next();
	    float[] data = feature.getFeatureData();
	    for (int d = 0; d < data.length; d++) {
		outStream.writeFloat(data[d]);
	    }
	}

	outStream.close();
    }

    /**
     * Dumps the Cepstra to the given ascii output file.
     *
     * @param outputFile the ascii output file
     */
    public void dumpAscii(String outputFile) throws IOException {
	PrintStream ps = new PrintStream(new FileOutputStream(outputFile),
					 true);
	ps.print(getNumberDataPoints());<commit_msg>Fixed a bug that writes the number of data points in float rather than int.<commit_after>/*
 * Copyright 1999-2002 Carnegie Mellon University.  
 * Portions Copyright 2002 Sun Microsystems, Inc.  
 * Portions Copyright 2002 Mitsubishi Electronic Research Laboratories.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 *
 */


package tests.frontend;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

import java.net.URL;

import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;

import edu.cmu.sphinx.frontend.AudioSource;
import edu.cmu.sphinx.frontend.FrontEnd;
import edu.cmu.sphinx.frontend.Feature;
import edu.cmu.sphinx.frontend.FeatureExtractor;
import edu.cmu.sphinx.frontend.FeatureFrame;
import edu.cmu.sphinx.frontend.StreamAudioSource;

import edu.cmu.sphinx.util.SphinxProperties;


/**
 * This program takes in an audio file, does frontend signal processing to it,
 * and then dumps the resulting Cepstra into a separate binary file.
 *
 * This program takes three arguments:
 *
 * 1. propsFile - the Sphinx properties file
 * 2. audioFile - the name of the audio file
 * 3. outputFile - the name of the binary output file
 */
public class CepstrumFileDumper {

    private FrontEnd frontend;
    private List allFeatures;
    private int featureLength;
    
    /**
     * Constructs a CepstrumFileDumper.
     *
     * @param propsFile the Sphinx properties file
     * @param inputAudioFile the input audio file
     */
    public CepstrumFileDumper(String propsFile, String inputAudioFile)
	throws FileNotFoundException, IOException {
        try {
	    String context = "cepstralDumping";
	    String pwd = System.getProperty("user.dir");
	    SphinxProperties.initContext
		(context, new URL("file://" + pwd + File.separatorChar +
				  propsFile));
	    SphinxProperties props = SphinxProperties.getSphinxProperties
		(context);
	    featureLength = props.getInt
		(FeatureExtractor.PROP_FEATURE_LENGTH, 13);

	    AudioSource audioSource = new StreamAudioSource
		("BatchFileAudioSource", context,
		 (new FileInputStream(inputAudioFile)), inputAudioFile);

	    frontend = new FrontEnd("frontend", context, audioSource);

	    allFeatures = new LinkedList();
	    getAllFeatures();

	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * Retrieve all Features from the frontend, and cache all those
     * with actual feature data.
     */
    private void getAllFeatures() throws IOException {
	Feature feature = null;
	FeatureFrame frame = null;
	while ((frame = frontend.getFeatureFrame(1)) != null) {
	    feature = frame.getFeatures()[0];
	    if (feature.hasContent()) {
		allFeatures.add(feature);
	    }
	}
    }

    /**
     * Returns the total number of data points that should be written
     * to the output file.
     *
     * @return the total number of data points that should be written
     */
    private int getNumberDataPoints() {
	return (allFeatures.size() * featureLength);
    }

    /**
     * Dumps the Cepstra to the given binary output.
     *
     * @param outputFile the binary output file
     */
    public void dumpBinary(String outputFile) throws IOException {
	DataOutputStream outStream = new DataOutputStream
	    (new FileOutputStream(outputFile));
	outStream.writeInt(getNumberDataPoints());

	for (Iterator i = allFeatures.iterator(); i.hasNext();) {
	    Feature feature = (Feature) i.next();
	    float[] data = feature.getFeatureData();
	    for (int d = 0; d < data.length; d++) {
		outStream.writeFloat(data[d]);
	    }
	}

	outStream.close();
    }

    /**
     * Dumps the Cepstra to the given ascii output file.
     *
     * @param outputFile the ascii output file
     */
    public void dumpAscii(String outputFile) throws IOException {
	PrintStream ps = new PrintStream(new FileOutputStream(outputFile),
					 true);
	ps.print(getNumberDataPoints());
{'fraction_non_alphanumeric': 0.09408795962509012, 'fraction_numerical': 0.005767844268204758, 'mean_word_length': 3.4579539367970007, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6424479', 'n_tokens_mistral': 2555, 'n_tokens_neox': 2382, 'n_words': 732}