聆听时改变音频 [英] Change audio frequency while listening
问题描述
听音乐时是否可以改变音频(音调)?
我在谷歌搜索但我被告知转换/更改整个文件。我打算在听音乐的同时创建两个按钮(上下)来改变频率。
先谢谢。
Is it possible to change audio frequency (pitch) while listening to a music?
I searched in google but I was told to convert / change the whole file. I'm planning to create two button (up and down) to change the frequency while listening to a music.
Thanks before.
推荐答案
使用此代码读取wave文件:
Use this code to read the wave file:
// convert two bytes to one double in the range -1 to 1
static double bytesToDouble(byte firstByte, byte secondByte) {
// convert two bytes to one short (little endian)
short s = (secondByte << 8) | firstByte;
// convert to range from -1 to (just below) 1
return s / 32768.0;
}
// Returns left and right double arrays. 'right' will be null if sound is mono.
public void openWav(string filename, out double[] left, out double[] right)
{
byte[] wav = File.ReadAllBytes(filename);
// Determine if mono or stereo
int channels = wav[22]; // Forget byte 23 as 99.999% of WAVs are 1 or 2 channels
// Get past all the other sub chunks to get to the data subchunk:
int pos = 12; // First Subchunk ID from 12 to 16
// Keep iterating until we find the data chunk (i.e. 64 61 74 61 ...... (i.e. 100 97 116 97 in decimal))
while(!(wav[pos]==100 && wav[pos+1]==97 && wav[pos+2]==116 && wav[pos+3]==97)) {
pos += 4;
int chunkSize = wav[pos] + wav[pos + 1] * 256 + wav[pos + 2] * 65536 + wav[pos + 3] * 16777216;
pos += 4 + chunkSize;
}
pos += 8;
// Pos is now positioned to start of actual sound data.
int samples = (wav.Length - pos)/2; // 2 bytes per sample (16 bit sound mono)
if (channels == 2) samples /= 2; // 4 bytes per sample (16 bit stereo)
// Allocate memory (right will be null if only mono sound)
left = new double[samples];
if (channels == 2) right = new double[samples];
else right = null;
// Write to double array/s:
int i=0;
while (pos < length) {
left[i] = bytesToDouble(wav[pos], wav[pos + 1]);
pos += 2;
if (channels == 2) {
right[i] = bytesToDouble(wav[pos], wav[pos + 1]);
pos += 2;
}
i++;
}
}
在内存中对c#数组进行修改:
改变音高意味着以更低或更高的采样率重新采样阵列,实际上是拉伸或收缩波形以调整频率。
将数组写入新的Wave文件中; >
有用的链接:https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
Make the modifications to the c# array in memory:
Changing pitch means re-sampling the array at a lower or higher sample rate, in effect stretching or shrinking the waveform to adjust the frequency.
Write out the array into a new Wave file;
Useful Link: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
然后你必须自己处理音频的播放 - 这是一个很多工作。
完成后,查看发送到音频设备的音频数据缓冲区:需要更改这些缓冲区。这意味着,您需要进行傅立叶变换以获取频率,然后移动频率,并将傅立叶变换返回到音频数据。
Then you have to handle the playback of audio yourself - that's a lot of work.
When you've accomplished that, take a look at the buffers of audio data you send to the audio device: these buffers need to be changed. That means, you do a Fourier transformation to get the frequencies, then shift the frequencies, and do the Fourier transformation back to the audio data.
这篇关于聆听时改变音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!