I wrote the following code to create a wav file which generates varying frequencies based on the Hebrew Gematria pattern. It compiles great but does not write the wav file. What am I not understanding about this code?

#include <iostream>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

#define SECONDS 10 /* produce 10 second(s) of noise */
#define PI 3.14159265358979
using namespace std;

int put_little_short(char *t, unsigned int value)
{
    *(unsigned char *)(t++)=value&255;
    *(unsigned char *)(t)=(value/256)&255;
    return 2;
}

int put_little_long(char *t, unsigned int value)
{
    *(unsigned char *)(t++)=value&255;
    *(unsigned char *)(t++)=(value/256)&255;
    *(unsigned char *)(t++)=(value/(256*256))&255;
    *(unsigned char *)(t)=(value/(256*256*256))&255;
    return 4;
}

/* returns the number of bytes written. skips two bytes after each write */
int fill_data(char *start, int frequency, int seconds)
{
    int i, len=0;
    double value;
    for(i=0; i<seconds*44100; i++) 
    {
        value=32767.0 * sin(2.0*PI*((double)(i))*(double)(frequency)/44100.0);
        put_little_short(start,int(value));
        start += 4;
        len+=2;
    }
    return len;
}

int power(int a , int b)
{
	int i=0;
	int product=1;
	for(i=0;i<b;i++)
		product*=a;

	return product;
}

int main()
{
    int heblen,switchlen=0,gemarray[1000],shellinput=0,shellarray[10000],shelloutput=0,sshelloutput=0,sshellinput=0,sshellarray[100000];
    string heb;
    
    cout<< "This is the hebrew letter scheme \t";
    cout<< "A-Alef \t";
    cout<< "B-Beth \t";
    cout<< "G-Gyml \t";
    cout<< "D-Dalet \t";
    cout<< "H-He \t";
    cout<< "V-Vau \t";
    cout<< "Z-Zain \t";
    cout<< "C-Chet \t";
    cout<< "T-Teth \t";
    cout<< "Y-Yod \t";
    cout<< "K-Kaf \t";
    cout<< "L-Lamed \t";
    cout<< "M-Mem \t";
    cout<< "N-Nun \t";
    cout<< "S-Samek \t";
    cout<< "I-Ain \t";
    cout<< "P-Pey \t";
    cout<< "O-Tsadhe \t";
    cout<< "Q-Qof \t";
    cout<< "R-Resh \t";
    cout<< "E-Shin \t";
    cout<< "F-Tav \t";
    cout<< "k-final Kaf\t";
    cout<< "m-final Mem\t";
    cout<< "n-final Nun\t";
    cout<< "p-final Pey\t";
    cout<< "o-final Tsadhe\t";
    cout<< "Input Hebrew phrase->\t";    
    
    getline(cin,heb);
    heblen=(sizeof heb)/(sizeof heb[0]);
    while (switchlen<=heblen)
          {
                switch(heb[switchlen])
                {
                       case 'A':
                       gemarray[switchlen]=1000; 
                       break;
                       case 'B': 
                       gemarray[switchlen]=2; 
                       break;
                       case 'G':
                       gemarray[switchlen]=3; 
                       break;
                       case 'D':
                       gemarray[switchlen]=4; 
                       break;
                       case 'H':
                       gemarray[switchlen]=5; 
                       break;
                       case 'V':
                       gemarray[switchlen]=6; 
                       break;
                       case 'Z':
                       gemarray[switchlen]=7; 
                       break;
                       case 'C':
                       gemarray[switchlen]=8; 
                       break;
                       case 'T':
                       gemarray[switchlen]=9; 
                       break;
                       case 'Y':
                       gemarray[switchlen]=10; 
                       break;
                       case 'K':
                       gemarray[switchlen]=20; 
                       break;
                       case 'L':
                       gemarray[switchlen]=30; 
                       break;
                       case 'M':
                       gemarray[switchlen]=40; 
                       break;
                       case 'N':
                       gemarray[switchlen]=50; 
                       break;
                       case 'S':
                       gemarray[switchlen]=60; 
                       break;
                       case 'I':
                       gemarray[switchlen]=70; 
                       break;
                       case 'P':
                       gemarray[switchlen]=80; 
                       break;
                       case 'O':
                       gemarray[switchlen]=90; 
                       break;
                       case 'Q':
                       gemarray[switchlen]=100; 
                       break;
                       case 'R':
                       gemarray[switchlen]=200; 
                       break;
                       case 'E':
                       gemarray[switchlen]=300; 
                       break;
                       case 'F':
                       gemarray[switchlen]=400; 
                       break;
                       case 'k':
                       gemarray[switchlen]=500; 
                       break;
                       case 'm':
                       gemarray[switchlen]=600; 
                       break;
                       case 'n':
                       gemarray[switchlen]=700; 
                       break;
                       case 'p':
                       gemarray[switchlen]=800; 
                       break;
                       case 'o':
                       gemarray[switchlen]=900; 
                       break;
                       default: 
                       gemarray[switchlen]=0;
                       break;         
                 }   
                          switchlen++;
          }
          while (shellinput<=switchlen)
          {
                int n=0, halfshell=gemarray[shellinput]/2,shell;
                
                if (gemarray[shellinput]<2)
                {
                      shellarray[shelloutput]=gemarray[shellinput];
                      gemarray[shellinput]-=shellarray[shelloutput];
                      
                      if (shellarray[shelloutput]>=1)
                      {
                           shellarray[shelloutput]=gemarray[shellinput];
                      }
                }
                if (gemarray[shellinput]>=2)   
                {
                      do
                      {
                             shell=2*power(n,2); 
                             if (gemarray[shellinput]<shell)
                             {
                                   break;
                             }
                             gemarray[shellinput]-=shell;
                             if (shell>=1)
                             {
                                  shellarray[shelloutput]=shell;
                             }
                             n++;
                             shelloutput++;
                      }
                      while (gemarray[shellinput]>=halfshell);
                      while (gemarray[shellinput]-shell>=1)
                      {
                            shell=2*power(n,2);
                            if (gemarray[shellinput]<shell)
                            {
                                   break;
                            }
                            gemarray[shellinput]-=shell;
                            if (shell>=1)
                            {
                                 shellarray[shelloutput]=shell;
                            }
                            n--;
                            shelloutput++;
                      }
                }
                shell=gemarray[shellinput];
                if (shell>=1)
                {
                      shellarray[shelloutput]=shell;
                }
                shellinput++;
          }
          while (sshellinput<=shelloutput)
          {
                int l=0,sshell=0,sshelloutput=0,;
                if (shellarray[sshellinput]<2)
                {
                      sshellarray[sshelloutput]=shellarray[sshellinput];
                      shellarray[sshellinput]-=sshellarray[sshelloutput]; 
                      if (sshellarray[shelloutput]>=1)
                      {
                            sshellarray[sshelloutput]=shellarray[sshelloutput];
                      }
                }
                if (shellarray[sshellinput]>=2)
                {
                    do
                    {
                         sshell=2*((2*l)+1);
                         if (shellarray[sshellinput]<sshell)
                         {
                              break;
                         } 
                         shellarray[sshellinput]-=sshell;    
                         if (sshell>=1)
                         {
                              sshellarray[sshelloutput]=sshell;
                         }
                         l++;
                         sshelloutput++;
                    }
                    while (shellarray[sshellinput]-sshell>=1);
                }
                if (shellarray[sshellinput]<sshell)
                {
                    sshell=shellarray[sshellinput];
                    if (sshell>=1)
                    {
                         sshellarray[sshelloutput]=sshell;
                    }
                }
                sshelloutput++;
          }
{
    char *buffer=(char *)malloc(SECONDS*44100*4+1000);
    char *t=buffer;
    int len,wavindex=0;
    int fd;

    *t++='R'; *t++='I'; *t++='F'; *t++='F';
    t+=4; /* total length will be put in later */
    *t++='W'; *t++='A'; *t++='V'; *t++='E';

    /* format chunk, 8 bytes header and 16 bytes payload */
    *t++='f'; *t++='m'; *t++='t'; *t++=' ';
    t+=put_little_long(t,16); /* I know the length  of the fmt_ chunk*/
    t+=put_little_short(t,1); /* chunk type is always one */
    t+=put_little_short(t,2); /* two channels */
    t+=put_little_long(t,44100); /* samples per second */
    t+=put_little_long(t,44100*2*2); /* bytes per second */
    t+=put_little_short(t,4); /* bytes pro sample (all channels) */
    t+=put_little_short(t,16); /* bits per sample */

    /* data chunk, 8 bytes header and XXX bytes payload */
    *t++='d'; *t++='a'; *t++='t'; *t++='a';

    while (sshellarray[wavindex]>=1)
    {
          len=fill_data(t+4,(528*sshellarray[wavindex]),(SECONDS/sshelloutput)); /* left channel, 528Hz sine */
          len+=fill_data(t+6,(528*sshellarray[wavindex]),(SECONDS/sshelloutput)); /* right channel, 528Hz sine */
          put_little_long(t,len);
          put_little_long(buffer+4,len+8+16+8);
          wavindex++;
    }
    fd=open("test.wav", O_RDWR|O_CREAT|ios::app, 0644);
    write(fd,buffer,len+8+16+8+8);
    close(fd);
    return 0;
}
}

Hey, gusano79 I'm not quite sure how to do that. Plz reply with a code snippet.

instead of calling open directly, assign it's result to a value like

int a; a = fopen(filename.wav) //etc

EDIT: In your case, just check

int fd

's value.

Hey, gusano79 I'm not quite sure how to do that. Plz reply with a code snippet.

Try to read and understand the documentation I linked; all of the information you need is there.

A slightly more detailed example:

fd=open("test.wav", O_RDWR|O_CREAT|ios::app, 0644);
if(fd == -1)
{
    // The open failed; do something useful.
}
else
{
    // The open succeeded; go ahead and write the data.
    write(fd,buffer,len+8+16+8+8);
    close(fd);
}

Insert your own code after the "do something useful" comment--it should at least report the error to the user.

I tried this code:

fd=open("test.wav", O_RDWR|O_CREAT, 0644);
    if(fd == -1)
    {
          // The open failed; do something useful.
          cout << "OPEN FAILED!";
    }
    else
    {
          // The open succeeded; go ahead and write the data.
          write(fd,buffer,len+8+16+8+8);
          close(fd);
    }

But still the program just hangs. Any other ideas?

If you do want to use your code in your project, you may debug it with setting breakpoints. It will help you find out where the hang code is.
Anyway, I go though you code quickly, and it looks not so efficient.

Find and make some reference from some related open sources http://www.becoding.com/source/wav/. That would be very helpful!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.