我正在做一个项目,我想通过组合不同的正弦波来产生(简单的)声音。我使用的是一个arduino mkrZero,因为它内置了I2S接口,而且它似乎有足够的处理能力来满足我的需要。
我的系统与arduino I2S simpleTone教程中的完全相同:

教程代码工作得很好,我从扬声器中得到了一个简单的方波音调。
现在我已经修改了代码以生成正弦波,这里有一个sin函数的查找表,以使它足够快:
#include <I2S.h>
uint16_t isin16[] = {
0, 1144, 2287, 3430, 4571, 5712, 6850, 7987, 9121, 10252, 11380,
12505, 13625, 14742, 15854, 16962, 18064, 19161, 20251, 21336, 22414,
23486, 24550, 25607, 26655, 27696, 28729, 29752, 30767, 31772, 32768,
33753, 34728, 35693, 36647, 37589, 38521, 39440, 40347, 41243, 42125,
42995, 43851, 44695, 45524, 46340, 47142, 47929, 48702, 49460, 50203,
50930, 51642, 52339, 53019, 53683, 54331, 54962, 55577, 56174, 56755,
57318, 57864, 58392, 58902, 59395, 59869, 60325, 60763, 61182, 61583,
61965, 62327, 62671, 62996, 63302, 63588, 63855, 64103, 64331, 64539,
64728, 64897, 65047, 65176, 65286, 65375, 65445, 65495, 65525, 65535,
}; //0-90
const int sincount = 2;
int freqs[] = {50*360,51*360};
float amps[] ={0.1,0.1};
const int sampleRate = 8000; // sample rate in Hz
short sample = 0;
double t = 0;
double dt = 1.0/(1.0*sampleRate);
short LR[] = {0,0};
void setup() {
Serial.begin(115200);
// start I2S at the sample rate with 16-bits per sample
if (!I2S.begin(I2S_PHILIPS_MODE, sampleRate, 16)) {
while (1); // do nothing
}
}
void loop() {
sample = 0;
for(int n= 0;n<sincount;n++)
{
sample += fSin(freqs[n]*t)*amps[n];
}
t += dt;
LR[0] = sample;
LR[1] = sample;
I2S.write(LR[0]);//left channel
I2S.write(LR[1]);//right channel
}
float fSin(long x)
{
boolean pos = true; // positive - keeps an eye on the sign.
if (x < 0)
{
x = -x;
pos = false;
}
if (x >= 360) x %= 360;
if (x > 180)
{
x -= 180;
pos = !pos;
}
if (x > 90) x = 180 - x;
if (pos) return isin16[x]; // = /65535.0
return isin16[x];
}这也很好。
但是!
如果我稍微修改一下代码,然后写
I2S.write(LR,2);而不是
I2S.write(LR[0]);//left channel
I2S.write(LR[1]);//right channel所有东西都坏了,扬声器发出的声音听起来像可怕的尖叫
来自I2S库参考:
描述 将二进制数据写入I2S接口。此数据以示例或一系列示例的形式发送。 语法
I2S.write(val)//阻塞I2S.write(buf, len)//非阻塞 参数 val:作为单个示例发送的值 buf:作为一系列示例发送的数组 len:缓冲器的长度 返回 字节写()将返回写入的字节数,尽管读取该数字是可选的。
我想使用写函数的后一个版本,因为它没有阻塞,而且我可以在前面的示例播放时生成新的示例。
对于如何使缓冲版本也正确工作,有什么想法吗?
发布于 2017-02-07 21:00:21
在您的代码中,您将LR声明为array of 2 short,Arduino中的short具有2 bytes大小。
当你写这个时:
I2S.write(LR[0]); //left channel
I2S.write(LR[1]); //right channel给定的
size_t I2SClass::write(uint8_t data)
{
return write((int32_t)data);
}
size_t I2SClass::write(int sample)
{
return write((int32_t)sample);
}您的short值应该自动被提升以适应int类型。现在,我对您正在处理的int的大小有点不确定,因为我找到的唯一I2S.h库是为SAMD board 大小而找到的,但通常在Arduino上,an int的大小为2 bytes。无论是哪种情况,都应按原样计算,而不存在任何问题。
当你写这个时:
I2S.write(LR,2);给定的
size_t I2SClass::write(const uint8_t *buffer, size_t size)
{
return write((const void*)buffer, size);
}
size_t I2SClass::write(const void *buffer, size_t size)
{
...
written = _doubleBuffer.write(buffer, size);
...
}您的array of 2 short应该转换为const void *。
来自I2SDoubleBuffer中的这段代码
size_t I2SDoubleBuffer::write(const void *buffer, size_t size) {
size_t space = availableForWrite();
if (size > space) {
size = space;
}
if (size == 0) {
return 0;
}
memcpy(&_buffer[_index][_length[_index]], buffer, size);
_length[_index] += size;
return size;
}看起来_doubleBuffer不知道声明的示例的大小(您将其声明为16 bits),它只是将size字节从输入复制到内部缓冲区。
因此,我猜想真正发生的情况是,当您请求缓冲2 shorts时,实际上只有2 bytes被复制。
示例:
假设您的LR包含以下值
short LR[2] = { 0x89AB, 0xCDEF };这就是我想象的会发生的事情
I2S.write(LR[0]); // actual left sample is 0x89AB
I2S.write(LR[1]); // actual right sample is 0xCDEF
I2S.write(LR, 2); // actual left sample is 0x89AB
// 0xCDEF is not copied
/* next loop iteration */
I2S.write(LR, 2); // actual right sample is 0x89AB
// 0xCDEF is not copied提出的解决方案:
试着要求复制4 bytes:
I2S.write(LR, 4); // actual left sample is 0x89AB
// actual right sample is 0xCDEFhttps://stackoverflow.com/questions/42097910
复制相似问题