unsigned char's

hey,

I havent used unsigned char's a lot but need to get something finished fast so dont have the time to get aquanted... I have an app that is a text to speach simulator and I need to concatenate sounds to create a word.  The sound data is stored with unsigned char's(BYTE*) and I'm unsure as to how I can build the word.  I've tryed using CStrings to do the job instead, but the data hasen't come out well and the sound player gets errors. Heres where I'm adding the sounds. (sorry if it doesnt format well.)


CString word;            //word data
int phonemeNum = 0;            //Num of phoneme to add to word
int from = 0;      //pointer to start of current phoneme
int wordLength = 0;            //word char length
CString input;

.
.
.

while(from < input.GetLength())
                  {
                        //speech synthesis
                        phonemeNum = get_phoneme(input, from);

                        //debug
                        cout << phonemeNum << " ";

                        int idx = phonemeNum;

                        //Add phonemes together to create word (Overlap phonemes)
                        //if space phoneme, play word... clear word...
                        if (phonemeNum != 52){      
                                    //*** If not space *** (add phoneme to word)
                              int len = phoneme[idx].get_len();      //get length
                              wordLength = wordLength + len;            //add length to word length
                              unsigned char* data = new unsigned char[len];      //create char
                              memcpy(data, (unsigned char*)(phoneme[idx].get_data()), len);      //put data into char
                              CString temp = data;                        //convert datato CString
                              word = word + temp;                              //add data to end of word
                        }else{      //*** If space *** (play word)
                              unsigned char* wordData = new unsigned char[wordLength];      //create char the size of the word
                              LPCTSTR tempWordData = word;                                                //change word to const char
                              memcpy(wordData, (const char*)(tempWordData), wordLength);      //place word in unsigned char
                              
                              //cout << LPCTSTR(word);
                              //cout << wordLength;

                              /*****- Play Word -*****/
                              wave.AddRef();
                              wave.SetFormat(      phoneme[0].get_rate(),                  // sample rate
                                                      phoneme[0].get_bytepersec(),            // data rate
                                                      phoneme[0].get_bytesample(),            // bytes/sam,
                                                      phoneme[0].get_bitssample());            // bits/sam
                              wave.Load(wordData, wordLength, phoneme[0].get_bitssample());
                              wave.Play();
                              wave.Release();

                              wordLength = 0;
                              word = "";
                        }
                      }


This code is a bit sloppy... but I don't know how else I can concatenate the sounds(phonemes)...?
faironAsked:
Who is Participating?
 
itsmeandnobodyelseConnect With a Mentor Commented:
do this:

unsigned char* word[100000];
wordLength = 0;
.....

int length = phoneme[idx].get_len();
memcpy(&word[wordLength], (unsigned char*)(phoneme[idx].get_data()), length);
wordLength = wordLength + length;

That concatenates.

Regards, Alex
0
 
mrwad99Commented:
I dont see why you are using unsigned chars, and why you are creating them on the heap at that.  Why not do it with normal char[] and then use strcat to concatenate ?
0
 
faironAuthor Commented:
The problem is that I was given the code to do with how the sounds are stored etc and I'm trying to work with whats there... Currently the sound is stored as:  BYTE*            data;

and phoneme[idx].get_data() returns this data.

The CWave player takes an unsigned char* to play a sound.

I need to concatenate the data from 2(or more) "phoneme[idx].get_data()"'s to create a new unsigned char* to send to wave.load.

I created something simpler without CStrings below but it still didnt work:


unsigned char* word[100000];

                        if (phonemeNum != 52){

                              int length = phoneme[idx].get_len();
                              wordLength = wordLength + length;
                              memcpy(word, (unsigned char*)(phoneme[idx].get_data()), length);

                        }else{

                              wave.AddRef();
                              wave.SetFormat(      phoneme[0].get_rate(),                  // sample rate
                                                      phoneme[0].get_bytepersec(),            // data rate
                                                      phoneme[0].get_bytesample(),            // bytes/sam,
                                                      phoneme[0].get_bitssample());            // bits/sam
                              unsigned char* data = new unsigned char[wordLength];
                              memcpy(data, (unsigned char*)(word), wordLength);
                              wave.Load(data, wordLength, phoneme[0].get_bitssample());
                              wave.Play();
                              wave.Release();

                        }
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mrwad99Commented:
Right,  you can construct a CString from your unsigned char[] like this:

CString str(phoneme[idx].get_data());

and then concatenate two CStrings in the normal + manner. This gives the correct result.
0
 
faironAuthor Commented:
Thats basically what I was doing before... but I dont think thats the problem.

How is the best way to turn the CString back into an unsigned char[]..?
0
 
mrwad99Commented:
..Just looking into that now...
0
 
mrwad99Commented:
There is some information here

http://www.gamedev.net/community/forums/topic.asp?topic_id=213156

although it might not be too much use.
0
 
mrwad99Commented:
..although it now looks like you can concatenate two unsigned char [] arrays together with strcat if you convert them to char* first.

This appears to give the correct result.  Try it.
0
 
itsmeandnobodyelseCommented:
As mrwad99 already told you

     CString temp(phoneme[idx].get_data());
     word += temp;    

is equivalent to

     unsigned char* data = new unsigned char[len];     //create char
     memcpy(data, (unsigned char*)(phoneme[idx].get_data()), len);     //put data into char
     CString temp = data;                    //convert datato CString
     word = word + temp;                         //add data to end of word
   
Depending on the type of get_data() member you may need a cast to char* .    

         CString temp((char*)phoneme[idx].get_data());

However, if the data contains binary data with binary zeros, both methods will fail as the CString constructor uses a strlen() function that stops copying at the first occurrence of a binary zero character. If this is a problem you may overcome this by that

     CString temp((char*)phoneme[idx].get_data(), len);   // the buffer must not be null-terminated and may contain binary null characters.
     word += temp;    

But,  if you cannot use that CString buffer as an argument when a null-terminating string is required, e. g. with cout << temp;    Here output stops at the first occurence of a zero character.

Regards, Alex

 
0
 
CayceCommented:
That the sounds are returned on a BYTE* doesn't mean that they're all printable characters. So parsing it to CString it's not a good idea. The sound is on a BYTE* Buffer and it could contain 0x00 characters, I guess it should be treated as a block of memory and not as a string of chars.
0
 
faironAuthor Commented:
" I guess it should be treated as a block of memory and not as a string of chars."

how can i do this?

and why wouldnt this work?

unsigned char* word[100000];
int length = phoneme[idx].get_len();
wordLength = wordLength + length;
memcpy(word, (unsigned char*)(phoneme[idx].get_data()), length);
0
 
mrwad99Commented:
Well that would work, but it would not achieve what you want which is unsigned char concatenation.  All memcpy does is copy one memory block to another, overwriting anything that is there.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.