-
Notifications
You must be signed in to change notification settings - Fork 587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds FeatureExtractor and corresponding test class; #37
Open
vladisav
wants to merge
3
commits into
cmusphinx:master
Choose a base branch
from
vladisav:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
sphinx4-core/src/main/java/edu/cmu/sphinx/api/FeatureExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package edu.cmu.sphinx.api; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import edu.cmu.sphinx.frontend.Data; | ||
import edu.cmu.sphinx.frontend.DataEndSignal; | ||
import edu.cmu.sphinx.frontend.DataProcessingException; | ||
import edu.cmu.sphinx.frontend.DoubleData; | ||
import edu.cmu.sphinx.frontend.FloatData; | ||
import edu.cmu.sphinx.frontend.FrontEnd; | ||
|
||
/** | ||
* Extracts features from input stream | ||
* | ||
* @author Vladisav Jelisavcic | ||
* | ||
*/ | ||
class FeatureExtractor { | ||
|
||
private FrontEnd frontEnd; | ||
private int featureLength = -1; | ||
|
||
/** | ||
* Constructs a FeatureExtractor. | ||
* | ||
* @param cm | ||
* the configuration manager | ||
* @param frontEndName | ||
* the name for the frontEnd to be used | ||
* @param inputStream | ||
* data stream | ||
* | ||
* @throws IOException | ||
* if error occurred | ||
*/ | ||
public FeatureExtractor(InputStream inputStream, int sampleRate) | ||
throws IOException { | ||
Configuration configuration = new Configuration(); | ||
|
||
configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us"); | ||
configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict"); | ||
configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin"); | ||
|
||
Context ctx = new Context(configuration); | ||
ctx.setSampleRate(sampleRate); | ||
ctx.setSpeechSource(inputStream); | ||
|
||
frontEnd = (FrontEnd) ctx.getInstance(FrontEnd.class); | ||
} | ||
|
||
/** | ||
* Extracts all features from the supplied InputStream. | ||
* | ||
* @return float[][] when called first time, null otherwise | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type was changed to a List. The javadoc should be updated. |
||
* | ||
* @throws DataProcessinException | ||
* if error occurred | ||
*/ | ||
public List<float[]> getAllFeatures() throws DataProcessingException { | ||
List<float[]> featureList = new ArrayList<float[]>(); | ||
|
||
Data feature = frontEnd.getData(); | ||
if (feature == null) | ||
return null; | ||
|
||
while (!(feature instanceof DataEndSignal)) { | ||
if (feature instanceof DoubleData) { | ||
double[] featureData = ((DoubleData) feature).getValues(); | ||
if (featureLength < 0) { | ||
featureLength = featureData.length; | ||
} | ||
float[] convertedData = new float[featureData.length]; | ||
for (int i = 0; i < featureData.length; i++) { | ||
convertedData[i] = (float) featureData[i]; | ||
} | ||
featureList.add(convertedData); | ||
} else if (feature instanceof FloatData) { | ||
float[] featureData = ((FloatData) feature).getValues(); | ||
if (featureLength < 0) { | ||
featureLength = featureData.length; | ||
} | ||
featureList.add(featureData); | ||
} | ||
feature = frontEnd.getData(); | ||
} | ||
|
||
return featureList; | ||
} | ||
|
||
/** | ||
* Extracts a single feature frame from the supplied InputStream. | ||
* | ||
* @return float[] or null if end of stream reached | ||
* | ||
* @throws DataProcessinException | ||
* if error occurred | ||
*/ | ||
public float[] getNextFeatureFrame() throws DataProcessingException { | ||
Data feature = frontEnd.getData(); | ||
if (feature == null) | ||
return null; | ||
|
||
while (!(feature instanceof DoubleData || feature instanceof FloatData)) { | ||
feature = frontEnd.getData(); | ||
if (feature == null) | ||
return null; | ||
} | ||
|
||
if (feature instanceof DoubleData) { | ||
double[] featureData = ((DoubleData) feature).getValues(); | ||
if (featureLength < 0) { | ||
featureLength = featureData.length; | ||
} | ||
float[] convertedData = new float[featureData.length]; | ||
for (int i = 0; i < featureData.length; i++) { | ||
convertedData[i] = (float) featureData[i]; | ||
} | ||
|
||
return convertedData; | ||
} else if (feature instanceof FloatData) { | ||
float[] featureData = ((FloatData) feature).getValues(); | ||
if (featureLength < 0) { | ||
featureLength = featureData.length; | ||
} | ||
|
||
return featureData; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} | ||
|
101 changes: 101 additions & 0 deletions
101
sphinx4-core/src/test/java/edu/cmu/sphinx/api/FeatureExtractorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* | ||
*/ | ||
package edu.cmu.sphinx.api; | ||
|
||
import static org.testng.AssertJUnit.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
import org.testng.annotations.AfterTest; | ||
import org.testng.annotations.BeforeTest; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* @author Vladisav Jelisavcic | ||
* | ||
*/ | ||
public class FeatureExtractorTest { | ||
|
||
static final String FRONTEND_NAME = "cepstraFrontEnd"; | ||
private float[] features; | ||
final int sampleRate = 16000; | ||
|
||
@BeforeTest | ||
public void setUp() { | ||
InputStream asciiStream = FeatureExtractorTest.class | ||
.getResourceAsStream("/edu/cmu/sphinx/api/10001-90210-01803.features"); | ||
Scanner sc = new Scanner(asciiStream); | ||
int numDataPoints = sc.nextInt(); | ||
|
||
features = new float[numDataPoints]; | ||
int i = 0; | ||
while (sc.hasNextFloat()) { | ||
features[i++] = sc.nextFloat(); | ||
} | ||
sc.close(); | ||
} | ||
|
||
@AfterTest | ||
public void tearDown() { | ||
|
||
} | ||
|
||
/** | ||
* Test method for | ||
* {@link edu.cmu.sphinx.api.FeatureExtractor#getAllFeatures()}. | ||
* | ||
* @throws IOException | ||
*/ | ||
@Test | ||
public void testGetAllFeatures() throws IOException { | ||
InputStream audioStream = FeatureExtractorTest.class | ||
.getResourceAsStream("/edu/cmu/sphinx/tools/bandwidth/10001-90210-01803.wav"); | ||
|
||
FeatureExtractor fe = new FeatureExtractor(audioStream, sampleRate); | ||
List<float[]> data = fe.getAllFeatures(); | ||
|
||
int numSamples = data.size(); | ||
int numFeatures = data.get(0).length; | ||
|
||
int numDataPoints = numSamples * numFeatures; | ||
|
||
assertEquals(features.length, numDataPoints); // check if all data | ||
// points are loaded | ||
for (int i = 0; i < numSamples; i++) { | ||
for (int j = 0; j < numFeatures; j++) { | ||
assertEquals(features[i * numFeatures + j], data.get(i)[j]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Test method for | ||
* {@link edu.cmu.sphinx.api.FeatureExtractor#getNextFeatureFrame()}. | ||
* | ||
* @throws IOException | ||
*/ | ||
@Test | ||
public void testGetNextFeatureFrame() throws IOException { | ||
InputStream audioStream = FeatureExtractorTest.class | ||
.getResourceAsStream("/edu/cmu/sphinx/tools/bandwidth/10001-90210-01803.wav"); | ||
|
||
FeatureExtractor fe = new FeatureExtractor(audioStream, sampleRate); | ||
|
||
int numDataPoints = 0; | ||
float[] data; | ||
while ((data = fe.getNextFeatureFrame()) != null) { | ||
for (int i = 0; i < data.length; i++) { | ||
assertEquals(features[i + numDataPoints], data[i]); | ||
} | ||
numDataPoints += data.length; | ||
} | ||
|
||
assertEquals(features.length, numDataPoints); // check if all data points are loaded | ||
} | ||
|
||
} | ||
|
1 change: 1 addition & 0 deletions
1
sphinx4-core/src/test/resources/edu/cmu/sphinx/api/10001-90210-01803.features
Large diffs are not rendered by default.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The javadoc params don't match the actual parameters.