简单记录,采样频率更改。


录制的音频为8000hz 单声道 改为44100hz双声道 

录制时将数据用byte【】写入了temp.pcm 播放是从中读取数据并播放。

主要需要改变采样率。

private short[] getStereoData(byte[] b) {
		if (b == null || b.length <= 3) {
			// throw new IllegalStateException("audio data is too short");
			return null;
		}
		short s[] = new short[b.length / 2];
		for (int i = 0; i < s.length; i++) {
			int j = (i * 2);
			s[i] = (short) (((b[j + 1] << 8) | b[j + 0] & 0xff));
		}
		return getStereoData(s);
	}

	private short[] getStereoData(short[] buffer) {

		if (buffer == null || buffer.length < 2) {
			return null;
		}
		float f = 44100.0f / 8000.0f * 2;
		int[] insertpos = new int[buffer.length];
		short[] b = new short[(int) (buffer.length * f)];
		for (int i = 0; i < buffer.length; i++) {
			int j = (int) (i * f);
			b[j] = buffer[i];
			insertpos[i] = j;
		}
		for (int i = 1; i < insertpos.length; i++) {
			short start = b[insertpos[i - 1]];
			short end = b[insertpos[i]];
			int num = insertpos[i] - insertpos[i - 1];
			int m = start;
			int n = end;
			int a = (n - m) / num;
			short average = (short) a;
			for (int j = 1; j < num; j++) {
				b[insertpos[i - 1] + j] = (short) (b[insertpos[i - 1]] + average * j);
			}
		}
		return b;
	}

主要是需要把byte值转换为short并插值。

发布了13 篇原创文章 · 获赞 1 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览