-8

update 2

I want to make a program that can play sound (that can say red, green and blue) on my pic 18f4550 there is a speaker connected to the picdem, that part works fine, I wrote the following program with microchip version 6.83 with the C compiler.

I need to retrieve the bits value of a .wav file, when I say red (this has a bit pattern). My right question, how can a get the bit value of my .wav file.

void main (void)
{   

    TRISD = 0x00;               // PORTD  als uitgang
    TRISB = 0b00110000;         // RB4 en RB5 als ingang
    TRISA = 0x00;               // RA output
        
    RCONbits.IPEN = 0;  // interrupts
    INTCONbits.GIE = 1; 
    INTCONbits.RBIE = 1;
    
    while(1)
    {   
        _asm sleep _endasm  
    }
}

#pragma interrupt ISR
void ISR (void)
{
    int red[] = {bit values off sound red???};
    int blue[] = {bit values off sound green???};
    int green[] = {bit values off sound blue???};


    if (INTCONbits.RBIF==1)
    {
        if(PORTBbits.RB5==0)        // S3 pressed?
        {
            int i = 0;
            int b;
            do {
                LATAbits.LATA2 = rood[i];
                LATDbits.LATD1 ^= 1;
                b = 0;

                //do-while voor de frequentie (1500 is de freq)
                do {
                    b++;
                }while(b <= 2000);

                i++;
            }while(rood[i] <= 50);

            //LATDbits.LATD1 ^= 1;      // D2 togglen

        }
        
    }   
    INTCONbits.RBIF = 0;
}
Community
  • 1
  • 1
projecten1
  • 67
  • 9
  • No, your question is not at all clear. So you want to play pre-recorded words? You will need to find some way to store the waveform data. – Oliver Charlesworth May 02 '11 at 16:52
  • No, I want just to pronounce Red, green or blue. but red must be in bit values, the same for the other 2 colors. – projecten1 May 02 '11 at 16:53
  • @user: Yes, so the big problem is: how do you get a PIC to play-back a pre-recorded audio waveform? – Oliver Charlesworth May 02 '11 at 16:54
  • look at this program on this link. this convert 'text' to his equal bit values. But I must have the 'color => red' in his equal bit values. http://nickciske.com/tools/binary.php – projecten1 May 02 '11 at 17:04
  • @user: I really don't understand what you're trying to explain. Are you saying that you want to get an array of 1s and 0s, which represent the bits in the ASCII for e.g. "red"? – Oliver Charlesworth May 02 '11 at 17:06
  • Yes, that's correct. Are there programs to do that? – projecten1 May 02 '11 at 17:09
  • @user: What do you mean by the "frequency of red, in bits"? So if the frequency is, for example, 13, you want `{1, 1, 0, 1}`? – Oliver Charlesworth May 02 '11 at 18:14
  • Yes, you can read hear. it's defined in nanometers.. http://en.wikipedia.org/wiki/Light – projecten1 May 02 '11 at 18:18
  • @user: It's ok, I understand the concept of frequency and wavelength! What I'm struggling to understand is what *you want to do* with a particular value. – Oliver Charlesworth May 02 '11 at 18:25
  • The question remains entirely unclear. An electromagnetic wave that a human would perceive as "red" covers a band of frequencies, there is no single frequency for red. But even if there were, the question makes little sense. Outputting the sound of the word "red" and its electromagnetic frequency are entirely unrelated. – Clifford May 02 '11 at 18:26
  • OK, sorry for my terrible english... Well the bits data represent then the color red and these data must be pronounced via the picdem through the boxes I connected to. – projecten1 May 02 '11 at 18:28
  • @user: Let me see if I understand you correctly this time! Your program will receive the value of the frequency/wavelength. You then want it to classify that value as e.g. red, green, blue. You then want it to generate an audio waveform to say that category, e.g. "red", "green", "blue". Is that the whole story? – Oliver Charlesworth May 02 '11 at 18:35
  • Your interpretation is fine now. But, i completed with the following explanation. I wrote previously a program that can determine which color I get, if I have a red object against the leds... Now this program above must speak the color in thix example red. The problem is to translate the frequency/wavelenth in bits. like the example that can, convert the word (text) red I want just the same buth for the light red 'value' represented in bits. I just want red represented in bits. – projecten1 May 02 '11 at 18:49
  • @user: Ok. So you have already solved the problem of wavelength -> colour category? (e.g. 660nm -> "red") So your remaining problem is translating "red" into an audio waveform? This really has *nothing* to do with the representing red "in bits". – Oliver Charlesworth May 02 '11 at 19:10
  • Yes.. The question is the representation of red in bits? how to set this in int rood[] = {...}; – projecten1 May 02 '11 at 19:32
  • @user: Does my answer below do what you want? – Oliver Charlesworth May 02 '11 at 21:15
  • had moved my explanation completely wrong. sorry for the inconvenience! – projecten1 May 04 '11 at 13:38

3 Answers3

2

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.

Martin
  • 2,442
  • 14
  • 15
  • OK, I update my question. had moved my explanation completely wrong. sorry for the inconvenience! – projecten1 May 04 '11 at 13:37
  • Do I realy load the .wav file in the pic18f4550? I want just the bit pattern of red. Is there a way to retrieve this, without loading the wave file in the picdem? – projecten1 May 04 '11 at 14:58
  • @user718910 Retrieve it from where? Your PC? Your PC's hard disk? A memory card? – Martin May 04 '11 at 15:35
  • @Martin and others. Try to take my explanetion literaly. 1) I take and speek in Windows 'sound recorder' and I say red. 2) convert it to .wav file (This step can also be redundant). 3) retrieve the bit values off this .wav file. 4) past these bits in my C program. I don't know how to do stap 3 without reading .wav file in the picdem or without PWM signals. My question is, are there programs to disassemble .wav files? Or tools that gives me the bit representation of sound red? – projecten1 May 04 '11 at 17:54
  • @user718910 Here is [a program that does what you want](http://www.romanblack.com/picsound.htm) which I came across browsing [this question](http://electronics.stackexchange.com/questions/3577/generating-speech-using-ics) on electronics.stackexchange.com. – Martin May 05 '11 at 07:42
  • @user718910 The reason it has taken a few days for you to get a reasonable answer is that your question had lots of irrelevant information in it. If you wanted to play a sound file using a PIC, just say that. The fact that the sound is a recording of you saying "red" or "blue" is irrelevant. In fact, when Oli asked if you wanted to play pre-recorded words you said No. You also talked about frequency in nanometers, which is both wrong and completely misleading for your question. I just thought you might like to know why your question was voted down. – Martin May 05 '11 at 07:48
  • sorry for this! Thanks for your reply anyway! – projecten1 May 05 '11 at 09:51
1

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.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

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.

unwind
  • 391,730
  • 64
  • 469
  • 606