简体   繁体   English

如何将.wav文件转换为float数组(编辑float数组,例如添加两个信号)并返回到.wav文件而不进行白化

[英]How can I convert a .wav file to a float array ( edit the float array e.g. adding two signals) and back to a .wav file without whitenoise

I am trying to program an auralization via Ray-Tracing in processing. 我正在尝试通过Ray-Tracing在处理中编写可听化程序。 To edit a sample over the information from the Ray Tracer, i need to convert a .wav File (File-Format: PCM-signed,16bit,stereo,2 bytes/frame, little endian) to an Float Array. 要通过Ray Tracer的信息编辑样本,我需要将.wav文件(文件格式:PCM签名,16位,立体声,2字节/帧,小端)转换为浮点数组。

I converted the audio via an audioInputStream and a DataInputStream, where I am loading the audio into an byte Array. 我通过audioInputStream和DataInputStream转换音频,我将音频加载到一个字节数组中。

Then I convert the byte Array to a float array like this. 然后我将字节数组转换为像这样的浮点数组。

byte[] samples;
float[] audio_data = float(samples); 

When I convert the float Array back to a .wav File, I'm getting the sound of the original Audio-File. 当我将浮点数组转换回.wav文件时,我得到了原始音频文件的声音。

But when I'm adding another Float Array to the Original signal and convert it back to a. 但是当我在原始信号中添加另一个Float Array并将其转换回。 wav file via the method above(even if I'm adding the same signal), i get a white noise signal instead of the wanted signal (I can hear the original signal under the white noise modulated, but very very silent). 通过上面的方法wav文件(即使我添加相同的信号),我得到一个白噪声信号而不是有用信号(我可以听到白噪声调制下的原始信号,但非常非常沉默)。

I read about this problem before, that there can be problems by the conversion from the float array to a byte array. 我之前读到过这个问题,从float数组转换为字节数组可能会出现问题。 That's because float is a 32bit datatype and byte (in java) is only 16 bits and somehow the bytes get mixed together wrong so the white noise is the result. 那是因为float是一个32位数据类型,而byte(在java中)只有16位,并且不知何故,字节混合在一起是错误的,因此白噪声就是结果。 In Processing there is a data type with signed 16bit integers (named: "short") but i can't modify the amplitude anymore, because therefore i need float values, which i can't convert to short. 在Processing中有一个带有符号16位整数的数据类型(命名为“short”),但我不能再修改幅度,因为我需要浮点值,我无法将其转换为short。

I also tried to handle the overflow (amplitude) in the float array by modulating the signal from 16 bit values (-32768/32767) to values from -1/1 and back again after mixing (adding) the signals. 我还尝试通过将信号从16位值(-32768/32767)调制到-1/1的值并在混合(添加)信号后再次返回来处理浮点数组中的溢出(幅度)。 The result gave me white noise. 结果给了我白噪声。 When i added more than 2 signals it gaves me nothing (nothing to hear). 当我添加2个以上的信号时,它什么也没给我任何东西(没什么可听的)。

The concrete Problem I want to solve is to add many signals (more than 1000 with a decent delay to create a kind of reverbation) in the form of float Arrays. 我想要解决的具体问题是以float Arrays的形式添加许多信号(超过1000个具有相当大的延迟以创建一种混响)。 Then I want to combine them to one Float Array that i want to save as an audio file without white noise. 然后我想将它们组合成一个Float Array,我想将其保存为没有白噪声的音频文件。

I hope you guys can help me. 我希望你们能帮助我。

If you have true PCM data points, there should be no problem using simple addition. 如果你有真正的PCM数据点,使用简单的加法应该没有问题。 The only issue is that on rare occasions (assuming your audio is not too hot to begin with) the values will go out of range. 唯一的问题是,在极少数情况下(假设您的音频不是太热,开始时)值将超出范围。 This will tend create a harsh distortion, not white noise. 这将产生严重的失真,而不是白噪声。 The fact that you are getting white noise suggests to me that maybe you are not converting your PCM sums back to bytes correctly for the format that you are outputting. 您遇到白噪声这一事实告诉我,您可能没有正确地将PCM总和转换回正确的输出格式。

Here is some code I use in AudioCue to convert PCM back to bytes. 下面是我在AudioCue中使用的一些代码,用于将PCM转换回字节。 The format is assumed to be 16-bit, 44100 fps, stereo, little-endian. 假设格式为16位,44100 fps,立体声,小端。 I'm working with PCM as normalized floats. 我正在使用PCM作为标准化浮点数。 This algorithm does the conversion for a buffer's worth of data at a time. 该算法一次转换缓冲区的数据。

for (int i = 0, n = buffer.length; i < n; i++)
    {
        buffer[i] *= 32767;

        audioBytes[i*2] = (byte) buffer[i];
        audioBytes[i*2 + 1] = (byte)((int)buffer[i] >> 8 );
    }

Sometimes, a function like Math.min(Math.max(audioval, -1), 1) or Math.min(Math.max(audioval, -32767), 32767) is used to keep the values in range. 有时,使用Math.min(Math.max(audioval,-1),1)或Math.min(Math.max(audioval,-32767),32767)等函数将值保持在范围内。 More sophisticated limiters or compressor algorithms will scale the volume to fit. 更复杂的限制器或压缩器算法将缩放音量以适应。 But still, if this is not handled, the result should be distortion, not white noise. 但是,如果不处理,结果应该是失真,而不是白噪声。

If the error is happening at another stage, we will need to see more of your code. 如果错误发生在另一个阶段,我们需要查看更多代码。

All this said, I wish you luck with the 1000-point echo array reverb. 所有这些说,祝你好运1000点回声阵列混响。 I hadn't heard of this approach working. 我没有听说过这种方法有效。 Maybe there are processors that can handle the computational load now? 也许现在有处理器可以处理计算负荷? (Are you trying to do this in real time?) My only success with coding real-time reverberation has been to use the Schroeder method, plugging the structure and values from the CCMRA Freeberb , working off of code from Craig Lindley's now ancient (copyright 2001) book "Digital Audio with Java". (您是否尝试实时执行此操作?)我在编码实时混响方面唯一的成功就是使用Schroeder方法,从CCMRA Freeberb中插入结构和值,使用Craig Lindley现在古老的代码(版权所有) 2001)书“用Java的数字音频”。 Most of that book deals with obsolete GUI code (pre-Swing!), but the code he gives for AllPass and Comb filters is still valid. 该书的大部分内容涉及过时的GUI代码(pre-Swing!),但他为AllPass和Comb过滤器提供的代码仍然有效。

I recall when I was working on this that I tracked down references a better reverb to try and code, but I would have to do some real digging to try and find my notes. 我记得当我正在研究这个时,我追踪了一个更好的混响尝试和编码,但我必须做一些真正的挖掘尝试找到我的笔记。 I was feeling over my head at the time, as the algorithm was presented via block diagrams not coding details or even pseudo-code. 我当时感觉很困惑,因为算法是通过框图来呈现的,而不是编码细节甚至是伪代码。 Would like to work on this again though and get a better reverb than the Shroeder-type to work. 想再次努力,并获得比Shroeder类型更好的混响工作。 The Schoeder was passable for sounds that were not too percussive. Schoeder可以通过那些不太打击的声音。

Getting a solution for real-time ray tracing would be a valuable accomplishment. 获得实时光线追踪的解决方案将是一项有价值的成就。 Many applications in AR/VR and games. AR / VR和游戏中的许多应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM