- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在构建一个相当简单的 Android 应用程序(sdk 修订版 14:ICS),它允许用户一次选择两个音频剪辑(都是 RIFF/WAV 格式,little-endian,签名的 PCM-16 位编码)和以各种方式组合它们以创造新的声音。我用于此组合的最基本方法如下:
//...sound samples are read in to memory as raw byte arrays elsewhere
//...offset is currently set to 45 so as to skip the 44 byte header of basic
//RIFF/WAV files
...
//Actual combination method
public byte[] makeChimeraAll(int offset){
for(int i=offset;i<bigData.length;i++){
if(i < littleData.length){
bigData[i] = (byte) (bigData[i] + littleData[i]);
}
else{
//leave bigData alone
}
}
return bigData;
}
然后可以通过 AudioTrack 类播放返回的字节数组:
....
hMain.setBigData(hMain.getAudioTransmutation().getBigData()); //set the shared bigData
// to the bigData in AudioTransmutation object
hMain.getAudioProc().playWavFromByteArray(hMain.getBigData(), 22050 + (22050*
(freqSeekSB.getProgress()/100)), 1024); //a SeekBar allows the user to adjust the freq
//ranging from 22050 hz to 44100 hz
....
public void playWavFromByteArray(byte[] audio,int sampleRate, int bufferSize){
int minBufferSize = AudioTrack.getMinBufferSize(sampleRate,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,
minBufferSize, AudioTrack.MODE_STREAM);
int i = 0;
at.play();
at.write(audio, 0, audio.length);
at.stop();
at.release();
for(i=0;i<audio.length;i++){
Log.d("me","the byte value at audio index " + i + " is " + audio[i]);
}
}
使用上面的代码组合和播放的结果接近我想要的(两个样本在生成的混合声音中仍然可以辨别)但也有很多裂纹、砰砰声和其他噪音。
那么,三个问题:首先,我是否正确使用了 AudioTrack?其次,字节顺序在 AudioTrack 配置中的什么位置?声音本身播放得很好,听起来几乎就像我在组合时所期望的那样,所以 RIFF/WAV 格式的小端性质似乎在某处传达,但我不确定在哪里。最后,对于带符号的 16 位 PCM 编码,我应该期望看到的字节值范围是多少?我希望在上面的 Log.d(...) 调用的 logcat 中看到从 −32768 到 32767 的值,但结果往往在 -100 到 100 的范围内(有一些异常值超出该范围)。超过 16 位范围的组合字节值是否可以解释噪声?
谢谢,CCJ
更新:主要感谢 Bjorne Roche 和 William the Coderer!我现在读入音频数据到 short[] 结构,DataInputStream 的字节顺序是使用 William 的 EndianInputStream (http://stackoverflow.com/questions/8028094/java-datainputstream-replacement-for-endianness) 和组合方法已更改为:
//Audio Chimera methods!
public short[] makeChimeraAll(int offset){
//bigData and littleData are each short arrays, populated elsewhere
int intBucket = 0;
for(int i=offset;i<bigData.length;i++){
if(i < littleData.length){
intBucket = bigData[i] + littleData[i];
if(intBucket > SIGNED_SHORT_MAX){
intBucket = SIGNED_SHORT_MAX;
}
else if (intBucket < SIGNED_SHORT_MIN){
intBucket = SIGNED_SHORT_MIN;
}
bigData[i] = (short) intBucket;
}
else{
//leave bigData alone
}
}
return bigData;
}
经过这些改进后的混合音频输出质量非常棒!
最佳答案
我不熟悉android音频,所以我不能回答你所有的问题,但我可以告诉你根本问题是什么:逐字节添加音频数据是行不通的。由于它有点工作,并且通过查看您的代码以及它最常见的事实,我将假设您有 16 位 PCM 数据。然而在任何地方,你都在处理字节。字节不适合处理音频(除非音频恰好是 8 位)
字节大约为 +/- 128。您说“我希望在上面的 Log.d(...) 调用的 logcat 中看到从 −32768 到 32767 的值,但结果往往在-100 到 100 的范围(还有一些异常值)”那么,当您从字节数组中打印值时,您怎么可能达到该范围? 16 位有符号数据的正确数据类型是短整型,而不是字节。如果您打印短值,您会看到预期的范围。
您必须将字节转换为短裤并对短裤求和。这将处理您听到的大部分杂音。但是,既然您正在阅读文件,为什么还要转换呢?为什么不使用类似这样的方法将其作为短片从文件中读出 http://docs.oracle.com/javase/1.4.2/docs/api/java/io/DataInputStream.html#readShort ()
下一个问题是您必须处理超出范围的值,而不是让它们“环绕”。最简单的解决方案是简单地将总和作为整数,“剪辑”到短范围内,然后存储剪辑后的输出。这将消除您的咔哒声和砰砰声。
在伪代码中,整个过程看起来像这样:
file1 = Open file 1
file2 = Open file 2
output = Open output for writing
numSampleFrames1 = file1.readHeader()
numSampleFrames2 = file2.readHeader()
numSampleFrames = min( numSampleFrames1, numSampleFrames2 )
output.createHeader( numSampleFrames )
for( int i=0; i<numSampleFrames * channels; ++i ) {
//read data from file 1
int a = file1.readShort();
//read data from file 2, and add it to data we read from file 1
a += file2.readShort();
//clip into range
if( a > Short.MAX_VALUE )
a = Short.MAX_VALUE;
if( a < Short.MIN_VALUE )
a = Short.MIN_VALUE;
//write it to the output
output.writeShort( (Short) a );
}
您会在“裁剪”步骤中得到一点失真,但没有简单的解决方法,而且裁剪比环绕要好得多。 (也就是说,除非你的音轨非常“热”,并且低频很重,否则失真应该不会太明显。如果这是一个问题,你可以做其他事情:例如将 a 乘以 .5 然后跳过剪裁,但随后您的输出会安静得多,这在手机上可能不是您想要的)。
关于android - 使用 Android 的 AudioTrack 组合声音样本的字节会产生噪音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11000933/
我在网上搜索但没有找到任何合适的文章解释如何使用 javascript 使用 WCF 服务,尤其是 WebScriptEndpoint。 任何人都可以对此给出任何指导吗? 谢谢 最佳答案 这是一篇关于
我正在编写一个将运行 Linux 命令的 C 程序,例如: cat/etc/passwd | grep 列表 |剪切-c 1-5 我没有任何结果 *这里 parent 等待第一个 child (chi
所以我正在尝试处理文件上传,然后将该文件作为二进制文件存储到数据库中。在我存储它之后,我尝试在给定的 URL 上提供文件。我似乎找不到适合这里的方法。我需要使用数据库,因为我使用 Google 应用引
我正在尝试制作一个宏,将下面的公式添加到单元格中,然后将其拖到整个列中并在 H 列中复制相同的公式 我想在 F 和 H 列中输入公式的数据 Range("F1").formula = "=IF(ISE
问题类似于this one ,但我想使用 OperatorPrecedenceParser 解析带有函数应用程序的表达式在 FParsec . 这是我的 AST: type Expression =
我想通过使用 sequelize 和 node.js 将这个查询更改为代码取决于在哪里 select COUNT(gender) as genderCount from customers where
我正在使用GNU bash,版本5.0.3(1)-发行版(x86_64-pc-linux-gnu),我想知道为什么简单的赋值语句会出现语法错误: #/bin/bash var1=/tmp
这里,为什么我的代码在 IE 中不起作用。我的代码适用于所有浏览器。没有问题。但是当我在 IE 上运行我的项目时,它发现错误。 而且我的 jquery 类和 insertadjacentHTMl 也不
我正在尝试更改标签的innerHTML。我无权访问该表单,因此无法编辑 HTML。标签具有的唯一标识符是“for”属性。 这是输入和标签的结构:
我有一个页面,我可以在其中返回用户帖子,可以使用一些 jquery 代码对这些帖子进行即时评论,在发布新评论后,我在帖子下插入新评论以及删除 按钮。问题是 Delete 按钮在新插入的元素上不起作用,
我有一个大约有 20 列的“管道分隔”文件。我只想使用 sha1sum 散列第一列,它是一个数字,如帐号,并按原样返回其余列。 使用 awk 或 sed 执行此操作的最佳方法是什么? Accounti
我需要将以下内容插入到我的表中...我的用户表有五列 id、用户名、密码、名称、条目。 (我还没有提交任何东西到条目中,我稍后会使用 php 来做)但由于某种原因我不断收到这个错误:#1054 - U
所以我试图有一个输入字段,我可以在其中输入任何字符,但然后将输入的值小写,删除任何非字母数字字符,留下“。”而不是空格。 例如,如果我输入: 地球的 70% 是水,-!*#$^^ & 30% 土地 输
我正在尝试做一些我认为非常简单的事情,但出于某种原因我没有得到想要的结果?我是 javascript 的新手,但对 java 有经验,所以我相信我没有使用某种正确的规则。 这是一个获取输入值、检查选择
我想使用 angularjs 从 mysql 数据库加载数据。 这就是应用程序的工作原理;用户登录,他们的用户名存储在 cookie 中。该用户名显示在主页上 我想获取这个值并通过 angularjs
我正在使用 autoLayout,我想在 UITableViewCell 上放置一个 UIlabel,它应该始终位于单元格的右侧和右侧的中心。 这就是我想要实现的目标 所以在这里你可以看到我正在谈论的
我需要与 MySql 等效的 elasticsearch 查询。我的 sql 查询: SELECT DISTINCT t.product_id AS id FROM tbl_sup_price t
我正在实现代码以使用 JSON。 func setup() { if let flickrURL = NSURL(string: "https://api.flickr.com/
我尝试使用for循环声明变量,然后测试cols和rols是否相同。如果是,它将运行递归函数。但是,我在 javascript 中执行 do 时遇到问题。有人可以帮忙吗? 现在,在比较 col.1 和
我举了一个我正在处理的问题的简短示例。 HTML代码: 1 2 3 CSS 代码: .BB a:hover{ color: #000; } .BB > li:after {
我是一名优秀的程序员,十分优秀!