除此之外,wav 文件中的位值可以通过获取文件中的每个样本(8 位?)并通过按位 AND 运算将掩码值 0x01 移动来确定设置了哪些位来获得。
Answer to original question: The "frequency of red, represent in bits" you asked for is 480 to 405 THz, so in bits that is 111100000 to 110010101. But you also said "its defined in nanometers", so that is 630 to 740nm which is 1001110110 to 1011100100 in binary. If you want to drive a loudspeaker with a waveform, you need either a sample audio waveform or a speech synthesis chip. In either case, the binary values I have given you have nothing to do with the sound of the word "red" which would need many more bits to represent any audible sound (eg a minimum of 0.5 second at 16 kbps PCM would be 8 kbp = 1 kbyte).
Another way representing a colour in binary is the RGB system where red would be 0xFF0000, which is 111111110000000000000000 in binary.
Answer to question on how to get bits in wav file First we have to determine where the wav file is stored. It could be stored as a const array in the PIC, or in an external memory device where you have to read it over some kind of serial or parallel bus. Since the PIC18F4550 has 32k bytes of flash, if your program is fairly small there could be enough room left for the 3 wav files.
Then we have to determine how the hardware is going to play the sound. You seem to be attempting to send a byte value out of bit port by shifting it. But to get this right we need to know more about the hardware, because you cannot connect a speaker to a bit port and expect sound out (not without further processing of a PCM signal anyway - are you attempting a 1 bit DAC design? If so, there are further notes here but this is quite ambitious).
Apart from that, the bit values in wav file, would be obtained by obtaining each sample (8 bit?) in the file and shifting a mask value of 0x01 round with a bitwise AND operation to determine which bits were set.
There is absolutely no correlation between the binary values that make up the encoding for a some letters of English, and the PCM values that would make up a sampled version of the sound of someone saying the encoded word.
If you want to play back the sound of someone saying "red", you will first have to sample it and store the resulting bits somewhere, then feed them to your output at an appropriate bitrate. The sampled sound is likely to be a lot larger than just the ASCII representation of "red" (which is 24 bits).
There are integrated chips that contain such samples and that actually can generate sound given an ASCII-encoded word, one example is this one. Unless you have such a chip connected to your MCU, your question is not making a lot of sense.
const char *str = "I hate programming";
// Loop over each character
for (i = 0; i < strlen(str); i++)
{
// Loop over each bit
for (j = 0; j < 8; j++)
{
// Extract bit #j of character i, using bit-shift and mask
bit = (str[i] >> j) & 1;
// bit will be 0 or 1, so do something with it here
}
}
如果您希望每个字符中的位顺序相反,则只需反转内部 (j) 循环。
请注意,正如 @unwind 在他的回答中所说,这与实际生成口语单词的音频波形无关。
Based on what you've said in your comments, it sounds like you want to get a stream of bits from the ASCII representation of a string like "red". You can do that by looping over each character of the string, and then looping over each bit position:
const char *str = "I hate programming";
// Loop over each character
for (i = 0; i < strlen(str); i++)
{
// Loop over each bit
for (j = 0; j < 8; j++)
{
// Extract bit #j of character i, using bit-shift and mask
bit = (str[i] >> j) & 1;
// bit will be 0 or 1, so do something with it here
}
}
If you want the bits in each character in the opposite order, then just reverse the order of the inner (j) loop.
Note that as @unwind says in his answer, this has nothing to do with actually generating an audio waveform for spoken words.
发布评论
评论(3)
原始问题的答案:
你要求的“红色频率,以位表示”是480到405太赫兹,所以以位为单位是111100000到110010101。但你也说“它以纳米定义”,所以这是630到740nm,即1001110110到1011100100以二进制形式。如果您想用波形驱动扬声器,您需要样本音频波形或语音合成芯片。无论哪种情况,我给你的二进制值都与“红色”一词的声音无关,这需要更多的位来表示任何可听声音(例如,16 kbps PCM 下至少 0.5 秒将是 8 kbp = 1 KB)。
另一种用二进制表示颜色的方法是 RGB 系统,其中红色为 0xFF0000,二进制表示为 111111110000000000000000。
回答有关如何获取 wav 文件中的位的问题
首先我们必须确定 wav 文件的存储位置。它可以作为 const 数组存储在 PIC 中,或者存储在外部存储设备中,您必须通过某种串行或并行总线读取它。由于 PIC18F4550 有 32k 字节的闪存,如果您的程序相当小,则可能有足够的空间容纳 3 个 wav 文件。
然后我们必须确定硬件将如何播放声音。您似乎试图通过移位将字节值从位端口发送出去。但为了做到这一点,我们需要更多地了解硬件,因为您无法将扬声器连接到位端口并期望发出声音(无论如何都不能不进一步处理 PCM 信号 - 您是否正在尝试 1 位 DAC 设计?如果有的话,有此处进一步说明 但这是相当雄心勃勃的)。
除此之外,wav 文件中的位值可以通过获取文件中的每个样本(8 位?)并通过按位 AND 运算将掩码值 0x01 移动来确定设置了哪些位来获得。
Answer to original question:
The "frequency of red, represent in bits" you asked for is 480 to 405 THz, so in bits that is 111100000 to 110010101. But you also said "its defined in nanometers", so that is 630 to 740nm which is 1001110110 to 1011100100 in binary. If you want to drive a loudspeaker with a waveform, you need either a sample audio waveform or a speech synthesis chip. In either case, the binary values I have given you have nothing to do with the sound of the word "red" which would need many more bits to represent any audible sound (eg a minimum of 0.5 second at 16 kbps PCM would be 8 kbp = 1 kbyte).
Another way representing a colour in binary is the RGB system where red would be 0xFF0000, which is 111111110000000000000000 in binary.
Answer to question on how to get bits in wav file
First we have to determine where the wav file is stored. It could be stored as a const array in the PIC, or in an external memory device where you have to read it over some kind of serial or parallel bus. Since the PIC18F4550 has 32k bytes of flash, if your program is fairly small there could be enough room left for the 3 wav files.
Then we have to determine how the hardware is going to play the sound. You seem to be attempting to send a byte value out of bit port by shifting it. But to get this right we need to know more about the hardware, because you cannot connect a speaker to a bit port and expect sound out (not without further processing of a PCM signal anyway - are you attempting a 1 bit DAC design? If so, there are further notes here but this is quite ambitious).
Apart from that, the bit values in wav file, would be obtained by obtaining each sample (8 bit?) in the file and shifting a mask value of 0x01 round with a bitwise AND operation to determine which bits were set.
构成某些英语字母的编码的二进制值与构成某人说编码单词的声音的采样版本的 PCM 值之间绝对没有相关性。
如果您想播放某人说“红色”的声音,您首先必须对其进行采样并将结果位存储在某处,然后以适当的比特率将它们输入到您的输出。采样的声音可能比“red”的 ASCII 表示(24 位)大得多。
有一些集成芯片包含此类样本,并且实际上可以在给定 ASCII 编码单词的情况下生成声音,一个例子是 这个。除非您的 MCU 连接了这样的芯片,否则您的问题没有多大意义。
There is absolutely no correlation between the binary values that make up the encoding for a some letters of English, and the PCM values that would make up a sampled version of the sound of someone saying the encoded word.
If you want to play back the sound of someone saying "red", you will first have to sample it and store the resulting bits somewhere, then feed them to your output at an appropriate bitrate. The sampled sound is likely to be a lot larger than just the ASCII representation of "red" (which is 24 bits).
There are integrated chips that contain such samples and that actually can generate sound given an ASCII-encoded word, one example is this one. Unless you have such a chip connected to your MCU, your question is not making a lot of sense.
根据您在评论中所说的内容,听起来您想从像
"red"
这样的字符串的 ASCII 表示形式中获取位流。您可以通过循环遍历字符串的每个字符,然后循环遍历每个位位置来做到这一点:如果您希望每个字符中的位顺序相反,则只需反转内部 (
j
) 循环。请注意,正如 @unwind 在他的回答中所说,这与实际生成口语单词的音频波形无关。
Based on what you've said in your comments, it sounds like you want to get a stream of bits from the ASCII representation of a string like
"red"
. You can do that by looping over each character of the string, and then looping over each bit position:If you want the bits in each character in the opposite order, then just reverse the order of the inner (
j
) loop.Note that as @unwind says in his answer, this has nothing to do with actually generating an audio waveform for spoken words.