`
wang_peng1
  • 浏览: 3942927 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

wav格式

 
阅读更多

http://alvinalexander.com/java/jwarehouse/android/core/java/android/speech/srec/WaveHeader.java.shtml

/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.speech.srec;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * This class represents the header of a WAVE format audio file, which usually
 * have a .wav suffix.  The following integer valued fields are contained:
 * <ul>
 * <li> format - usually PCM, ALAW or ULAW.
 * <li> numChannels - 1 for mono, 2 for stereo.
 * <li> sampleRate - usually 8000, 11025, 16000, 22050, or 44100 hz.
 * <li> bitsPerSample - usually 16 for PCM, 8 for ALAW, or 8 for ULAW.
 * <li> numBytes - size of audio data after this header, in bytes.
 * </ul>
 * 
 * Not yet ready to be supported, so
 * @hide
 */
public class WaveHeader {
    
    // follows WAVE format in http://ccrma.stanford.edu/courses/422/projects/WaveFormat

    private static final String TAG = "WaveHeader";
    
    private static final int HEADER_LENGTH = 44;
    
    /** Indicates PCM format. */
    public static final short FORMAT_PCM = 1;
    /** Indicates ALAW format. */
    public static final short FORMAT_ALAW = 6;
    /** Indicates ULAW format. */
    public static final short FORMAT_ULAW = 7;
    
    private short mFormat;
    private short mNumChannels;
    private int mSampleRate;
    private short mBitsPerSample;
    private int mNumBytes;
    
    /**
     * Construct a WaveHeader, with all fields defaulting to zero.
     */
    public WaveHeader() {
    }
    
    /**
     * Construct a WaveHeader, with fields initialized.
     * @param format format of audio data,
     * one of {@link #FORMAT_PCM}, {@link #FORMAT_ULAW}, or {@link #FORMAT_ALAW}. 
     * @param numChannels 1 for mono, 2 for stereo.
     * @param sampleRate typically 8000, 11025, 16000, 22050, or 44100 hz.
     * @param bitsPerSample usually 16 for PCM, 8 for ULAW or 8 for ALAW.
     * @param numBytes size of audio data after this header, in bytes.
     */
    public WaveHeader(short format, short numChannels, int sampleRate, short bitsPerSample, int numBytes) {
        mFormat = format;
        mSampleRate = sampleRate;
        mNumChannels = numChannels;
        mBitsPerSample = bitsPerSample;
        mNumBytes = numBytes;
    }
    
    /**
     * Get the format field.
     * @return format field,
     * one of {@link #FORMAT_PCM}, {@link #FORMAT_ULAW}, or {@link #FORMAT_ALAW}.
     */
    public short getFormat() {
        return mFormat;
    }
    
    /**
     * Set the format field.
     * @param format
     * one of {@link #FORMAT_PCM}, {@link #FORMAT_ULAW}, or {@link #FORMAT_ALAW}.
     * @return reference to this WaveHeader instance.
     */
    public WaveHeader setFormat(short format) {
        mFormat = format;
        return this;
    }
    
    /**
     * Get the number of channels.
     * @return number of channels, 1 for mono, 2 for stereo.
     */
    public short getNumChannels() {
        return mNumChannels;
    }
    
    /**
     * Set the number of channels.
     * @param numChannels 1 for mono, 2 for stereo.
     * @return reference to this WaveHeader instance.
     */
    public WaveHeader setNumChannels(short numChannels) {
        mNumChannels = numChannels;
        return this;
    }
    
    /**
     * Get the sample rate.
     * @return sample rate, typically 8000, 11025, 16000, 22050, or 44100 hz.
     */
    public int getSampleRate() {
        return mSampleRate;
    }
    
    /**
     * Set the sample rate.
     * @param sampleRate sample rate, typically 8000, 11025, 16000, 22050, or 44100 hz.
     * @return reference to this WaveHeader instance.
     */
    public WaveHeader setSampleRate(int sampleRate) {
        mSampleRate = sampleRate;
        return this;
    }
    
    /**
     * Get the number of bits per sample.
     * @return number of bits per sample,
     * usually 16 for PCM, 8 for ULAW or 8 for ALAW.
     */
    public short getBitsPerSample() {
        return mBitsPerSample;
    }
    
    /**
     * Set the number of bits per sample.
     * @param bitsPerSample number of bits per sample,
     * usually 16 for PCM, 8 for ULAW or 8 for ALAW.
     * @return reference to this WaveHeader instance.
     */
    public WaveHeader setBitsPerSample(short bitsPerSample) {
        mBitsPerSample = bitsPerSample;
        return this;
    }
    
    /**
     * Get the size of audio data after this header, in bytes.
     * @return size of audio data after this header, in bytes.
     */
    public int getNumBytes() {
        return mNumBytes;
    }
    
    /**
     * Set the size of audio data after this header, in bytes.
     * @param numBytes size of audio data after this header, in bytes.
     * @return reference to this WaveHeader instance.
     */
    public WaveHeader setNumBytes(int numBytes) {
        mNumBytes = numBytes;
        return this;
    }
    
    /**
     * Read and initialize a WaveHeader.
     * @param in {@link java.io.InputStream} to read from.
     * @return number of bytes consumed.
     * @throws IOException
     */
    public int read(InputStream in) throws IOException {
        /* RIFF header */
        readId(in, "RIFF");
        int numBytes = readInt(in) - 36;
        readId(in, "WAVE");

        /* fmt chunk */
        readId(in, "fmt ");
        if (16 != readInt(in)) throw new IOException("fmt chunk length not 16");
        mFormat = readShort(in);
        mNumChannels = readShort(in);
        mSampleRate = readInt(in);
        int byteRate = readInt(in);
        short blockAlign = readShort(in);
        mBitsPerSample = readShort(in);
        if (byteRate != mNumChannels * mSampleRate * mBitsPerSample / 8) {
            throw new IOException("fmt.ByteRate field inconsistent");
        }
        if (blockAlign != mNumChannels * mBitsPerSample / 8) {
            throw new IOException("fmt.BlockAlign field inconsistent");
        }

        /* data chunk */
        readId(in, "data");
        mNumBytes = readInt(in);
        
        return HEADER_LENGTH;
    }

    private static void readId(InputStream in, String id) throws IOException {
        for (int i = 0; i < id.length(); i++) {
            if (id.charAt(i) != in.read()) throw new IOException( id + " tag not present");
        }
    }

    private static int readInt(InputStream in) throws IOException {
        return in.read() | (in.read() << 8) | (in.read() << 16) | (in.read() << 24);
    }

    private static short readShort(InputStream in) throws IOException {
        return (short)(in.read() | (in.read() << 8));
    }

    /**
     * Write a WAVE file header.
     * @param out {@link java.io.OutputStream} to receive the header.
     * @return number of bytes written.
     * @throws IOException
     */
    public int write(OutputStream out) throws IOException {
        /* RIFF header */
        writeId(out, "RIFF");
        writeInt(out, 36 + mNumBytes);
        writeId(out, "WAVE");

        /* fmt chunk */
        writeId(out, "fmt ");
        writeInt(out, 16);
        writeShort(out, mFormat);
        writeShort(out, mNumChannels);
        writeInt(out, mSampleRate);
        writeInt(out, mNumChannels * mSampleRate * mBitsPerSample / 8);
        writeShort(out, (short)(mNumChannels * mBitsPerSample / 8));
        writeShort(out, mBitsPerSample);

        /* data chunk */
        writeId(out, "data");
        writeInt(out, mNumBytes);
        
        return HEADER_LENGTH;
    }

    private static void writeId(OutputStream out, String id) throws IOException {
        for (int i = 0; i < id.length(); i++) out.write(id.charAt(i));
    }

    private static void writeInt(OutputStream out, int val) throws IOException {
        out.write(val >> 0);
        out.write(val >> 8);
        out.write(val >> 16);
        out.write(val >> 24);
    }

    private static void writeShort(OutputStream out, short val) throws IOException {
        out.write(val >> 0);
        out.write(val >> 8);
    }
    
    @Override
    public String toString() {
        return String.format(
                "WaveHeader format=%d numChannels=%d sampleRate=%d bitsPerSample=%d numBytes=%d",
                mFormat, mNumChannels, mSampleRate, mBitsPerSample, mNumBytes);
    }

}

 

分享到:
评论

相关推荐

    wav格式报警铃声

    **wav格式报警铃声** WAV(Waveform Audio File Format)是微软与IBM共同开发的一种音频文件格式,属于无损音频格式,它保留了录音时的原始数据,因此音质非常出色,但相应的,文件体积也较大。在工业、安全、智能...

    .WAV格式。提示音

    .WAV格式,全称为Waveform Audio File Format,是由微软与IBM共同开发的一种音频文件格式,广泛应用于各种操作系统中。它是未经过压缩的“无损”音频格式,保留了原始录音的全部数据,因此声音质量非常高,但文件...

    报警铃音WAV格式45个.zip

    标题中的“报警铃音WAV格式45个.zip”表明这是一个包含45个WAV音频文件的压缩包,专门用于报警声音。WAV是Windows操作系统下的一种无损音频格式,通常用于高质量的声音录制和播放。这种格式保留了原始音频的所有细节...

    wav格式的报警声音

    标题中的“wav格式的报警声音”指的是音频文件类型,它使用了WAV(Waveform Audio File Format)这一标准。WAV是一种无损音频文件格式,由微软和IBM共同开发,广泛应用于各种操作系统中。该格式可以保留原始音频数据...

    提示音.wav格式

    标题中的“提示音.wav格式”指的是声音文件,具体来说是一种波形音频文件格式,WAV是微软公司开发的一种无损音频格式,广泛用于各种操作系统中。这种格式能精确地保存原始音频数据,但文件体积相对较大,不适合网络...

    PCM2WAV_pcm_wav_pcm和wav格式的互相转换_语音转换_

    WAV格式基于RIFF(Resource Interchange File Format)结构,支持多种编码方法,包括PCM。与PCM不同,WAV文件可以包含元数据,如采样率、位深度、通道数等,同时也可以包含有损压缩编码的数据。尽管WAV文件通常也...

    WAV格式报警铃声(90个)

    这个压缩包包含的是90个WAV格式的报警铃声,它们是专门为程序调用或其他类似应用设计的。 首先,让我们深入了解一下WAV格式。WAV是一种基于RIFF(Resource Interchange File Format)结构的文件格式,它可以存储...

    数字0-9中文语音wav格式音效

    本资源“数字0-9中文语音wav格式音效”聚焦于数字发音的音频文件,提供了0到9这十个数字的中文语音,且以WAV格式呈现。下面将详细解析这些知识点。 1. **数字发音**: 在人机交互中,数字的语音化至关重要,例如...

    96个报警铃音WAV格式

    本话题主要关注的是"96个报警铃音WAV格式"的音频资源包,它包含了96种不同的报警声音,每一种都是WAV格式,这是一种高质量的无损音频格式,适合用于专业级的音频处理和播放。 WAV(Waveform Audio Format)是微软和...

    wav格式音乐播放器(歌词显示)

    《基于UCOSII的WAV格式音乐播放器与歌词显示系统详解》 在嵌入式领域,音频播放器是常见的应用之一,特别是在嵌入式硬件平台如ARM3000上,开发一款功能丰富的音乐播放器是提升用户体验的重要手段。本项目主要探讨的...

    91个报警铃音WAV格式.rar

    标题中的“91个报警铃音WAV格式.rar”表明这是一个包含了91个不同报警铃声的压缩文件,这些铃声都是WAV音频格式。WAV(Waveform Audio Format)是微软和IBM共同开发的一种声音文件格式,它属于无损音频格式,能够高...

    wav格式的各种提示音,包括来电提示,短消息,完成提示,cs提示

    **wav格式介绍** wav,全称Waveform Audio Format,是由微软和IBM共同开发的一种声音文件格式,也是PC领域最常见、最原始的音频文件格式之一。这种格式的声音文件保真度非常高,因为它能无损地存储原始音频数据,但...

    83个常见报警音wav格式

    "83个常见报警音wav格式"这个资源集合提供了一组广泛使用的音频样本,它们都是以.wav为扩展名的音频文件。这种格式在行业内因其高质量和广泛的兼容性而被广泛采用。 首先,我们来了解一下.WAV文件格式。WAV...

    45个报警铃音WAV格式

    "45个报警铃音WAV格式"这个资源集合提供了一系列专为编程和开发用途设计的音频文件,主要特点是它们都采用WAV(Waveform Audio File Format)这种无损音频格式。 WAV是一种通用的音频文件格式,由微软和IBM共同开发...

    wav 声音 wav 格式

    标题中的“wav声音wav格式”指的是声音文件的一种常见格式,即Waveform Audio File Format,简称WAV。WAV是微软公司开发的一种无损音频格式,它以未经压缩的原始数据存储音频,因此提供了高质量的声音回放,但相应的...

    wav格式音频文件播放并显示波形

    本文将深入探讨如何播放和显示WAV格式的音频文件的波形,以及如何实现选择播放的功能,主要针对使用VC++(Visual C++)编程环境进行开发。 WAV是微软公司开发的一种无损音频文件格式,全称为Waveform Audio File ...

    amr、wav格式互转工具

    “amr、wav格式互转工具”是一款专为解决这两种格式转换问题设计的软件。该软件由索爱公司提供,索爱是一家知名的手机制造商,其产品在2000年代初非常流行。这款转换工具无需用户进行注册,意味着它是免费且易于使用...

    标准wav格式的音频文件

    本资源为标准的wav格式的音频资源,wav格式是音频中比较常见的格式,也是高清的音频数据,本资源供大家调试蓝牙音乐使用,如A2DP功能使用,对大家调试会有很大的帮助,欢迎从事音频和喜欢音频调试的朋友一起交流学习...

    整点报时语音包wav格式的文件

    "整点报时语音包wav格式的文件"是一个与音频处理相关的主题,主要涉及的是标准的音频文件格式——WAV(Waveform Audio Format)。在这个压缩包中,包含的文件名为“整点报时语音包”,我们可以推测这是一系列的WAV...

    按键音素材wav格式.rar

    标题中的“按键音素材wav格式.rar”表明这是一个包含多种按键音效的压缩文件,其主要格式为WAV。WAV是Windows操作系统下的一种无损音频文件格式,它以高质量记录声音,保留原始音频的所有细节,但文件体积相对较大。...

Global site tag (gtag.js) - Google Analytics