Java Code Examples for android.media.AudioTrack#MODE_STATIC

The following examples show how to use android.media.AudioTrack#MODE_STATIC . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AndroidAudioPlayer.java    From Android-Guitar-Tuner with Apache License 2.0 10 votes vote down vote up
public AndroidAudioPlayer(final AudioConfig audioConfig) {
    AudioAttributes audioAttributes = new AudioAttributes.Builder()
            .setLegacyStreamType(AudioManager.STREAM_MUSIC)
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build();

    AudioFormat audioFormat = new AudioFormat.Builder()
            .setChannelMask(audioConfig.getOutputChannel())
            .setEncoding(audioConfig.getOutputFormat())
            .setSampleRate(audioConfig.getSampleRate())
            .build();

    audioTrack = new AudioTrack(audioAttributes,
            audioFormat,
            audioConfig.getOutputBufferSize(),
            AudioTrack.MODE_STATIC,
            AudioManager.AUDIO_SESSION_ID_GENERATE);

    outputByteCount = audioConfig.getOutputFormatByteCount();
}
 
Example 2
Source File: AudioUtil.java    From Augendiagnose with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a sin wave of certain frequency and duration.
 *
 * @param freqHz     The frequency in Hertz
 * @param durationMs The duration in milliseconds
 * @return An AudioTrack with the corresponding sine wave.
 */
public static AudioTrack generateToneSine(final double freqHz, final int durationMs) {
	int count = (int) (BITRATE * 2.0 * (durationMs / MILLIS_IN_SECOND)) & ~1;
	short[] samples = new short[count];
	for (int i = 0; i < count; i += 2) {
		short sample = (short) (Math.sin(2 * Math.PI * i / (BITRATE / freqHz)) * 0x7FFF); // MAGIC_NUMBER
		samples[i] = sample;
		samples[i + 1] = sample;
	}
	@SuppressWarnings("deprecation")
	AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, (int) BITRATE,
			AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
			count * (Short.SIZE / 8), AudioTrack.MODE_STATIC); // MAGIC_NUMBER
	track.write(samples, 0, count);
	return track;
}
 
Example 3
Source File: AudioUtil.java    From Augendiagnose with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a sin wave of certain frequency and duration.
 *
 * @param freqHz     The frequency in Hertz
 * @param durationMs The duration in milliseconds
 * @return An AudioTrack with the corresponding sine wave.
 */
public static AudioTrack generateTonePulse(final double freqHz, final int durationMs) {
	int count = (int) (BITRATE * 2.0 * (durationMs / MILLIS_IN_SECOND)) & ~1;
	short[] samples = new short[count];
	for (int i = 0; i < count; i += 2) {
		short sample = TONE_MAP_2[(int) (2 * i / (BITRATE / freqHz)) % 2];
		samples[i] = sample;
		samples[i + 1] = sample;
	}
	@SuppressWarnings("deprecation")
	AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, (int) BITRATE,
			AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
			count * (Short.SIZE / 8), AudioTrack.MODE_STATIC); // MAGIC_NUMBER
	track.write(samples, 0, count);
	return track;
}
 
Example 4
Source File: AudioUtil.java    From Augendiagnose with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a tone of highest possible frequency.
 *
 * @param durationMs The duration in milliseconds
 * @return An AudioTrack with the corresponding sine wave.
 */
public static AudioTrack generateHighFreqTone(final int durationMs) {
	int count = (int) (BITRATE * 2.0 * (durationMs / MILLIS_IN_SECOND)) & ~1;
	short[] samples = new short[count];
	for (int i = 0; i < count; i += 2) { // MAGIC_NUMBER
		short sample = TONE_MAP[(i / 4) % 4]; // MAGIC_NUMBER
		samples[i] = sample;
		samples[i + 1] = sample;
	}
	@SuppressWarnings("deprecation")
	AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, (int) BITRATE,
			AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
			count * (Short.SIZE / 8), AudioTrack.MODE_STATIC); // MAGIC_NUMBER
	track.write(samples, 0, count);
	return track;
}
 
Example 5
Source File: FrequencyPlayer.java    From doppler-android with MIT License 5 votes vote down vote up
FrequencyPlayer(double frequency) {
    numSamples = sampleRate * duration / MILLIS_PER_SECOND;
    generatedSound = new byte[2 * numSamples];
    sample = new double[numSamples];

    audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, generatedSound.length, AudioTrack.MODE_STATIC);

    setFrequency(frequency);
}
 
Example 6
Source File: EuPlayer.java    From euphony with Apache License 2.0 4 votes vote down vote up
public void setSource(short[] src)
{
	mSource = src;
	mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, src.length*2, AudioTrack.MODE_STATIC);
}