int msgHandler(int sock) { // Header struct serbeep_header header; if (readHeader(sock, &header) != 0) return 1; if (header.magic != MAGIC) { fprintf(stderr, "Invalid magic\n"); return 1; } // clientHello if ((header.cmd & 0x2) == 0x2) { // serverHello header.cmd |= 0x1; if (writeStruct(sock, &header, sizeof(header)) != 1) return 1; return 0; } // musicScore if ((header.cmd & 0x4) == 0x4) { if (pthread_mutex_trylock(&global_score_mutex) != 0) return 1; if (readStruct(sock, &global_score_header, sizeof(global_score_header)) != 1) { pthread_mutex_unlock(&global_score_mutex); return 1; } global_score_header.length = ntohs(global_score_header.length); size_t size = sizeof(struct serbeep_score_note) * global_score_header.length; global_score_notes = (struct serbeep_score_note *)malloc(size); if (global_score_notes == NULL) { fprintf(stderr, "fail...malloc(%zd bytes)\n", size); pthread_mutex_unlock(&global_score_mutex); return 1; } if (readStruct(sock, global_score_notes, size) != 1) { freeNull(global_score_notes); pthread_mutex_unlock(&global_score_mutex); return 1; } // musicAck header.cmd |= 0x1; if (writeStruct(sock, &header, sizeof(header)) != 1) { freeNull(global_score_notes); pthread_mutex_unlock(&global_score_mutex); return 1; } pthread_mutex_unlock(&global_score_mutex); return 0; } return 0; // Success }
//=================================================================================================== Bool tests_readStruct(Bool details) { Bool testPassed = TRUE; Integ *myInteg, *newInteg; myInteg = malloc(sizeof(Integ)); newInteg = malloc(sizeof(Integ)); FILE* file = fopen("testStruct", "wb+"); if(file == NULL) { perror("copyFile fopen file"); return FALSE; } myInteg->a = 1; myInteg->b = 'a'; printf("%d%c", myInteg->a, myInteg->b); if(!writeStruct(file, &myInteg, sizeof(Integ))) testPassed = FALSE; fseek(file, 0, SEEK_SET); if(!readStruct(file, &newInteg, sizeof(Integ))) testPassed = FALSE; printf("\nNew int : %d and %c", newInteg->a, newInteg->b); free(myInteg); free(newInteg); fclose(file); return testPassed; }
int readHeader(int sock, struct serbeep_header *header) { if (readStruct(sock, header, sizeof(*header)) != 1) return 1; if (header->magic != MAGIC) return 1; if ((header->cmd & 0x1) == 0x1) return 1; return 0; }
// ############################################### void readProductList(void) { int i; int productList_fd; productList_fd = open(FILE_PATH, O_RDONLY); if (productList_fd < 0) { perror("Open error"); exit(1); } // End if for (i= 0; i < MAX_NUM_ITEMS; i++) { readStruct(productList_fd, &itemList[i]); } // End for } // End readProductList
SoundDescriptor * createDescriptor(FILE* file) { int doubleSize = sizeof(double), fSize = fileSize(file); double *tmp = malloc(sizeof(double)), *intervalsThreshold = malloc(sizeof(double) * globs_nbInterval); // + 1 because : an interval : |-----| : There is two thresholds SoundDescriptor * descriptor = malloc(sizeof(SoundDescriptor)); int iInterval, iElem, iWindow; // Increments int nbWindows = (fSize / doubleSize) / globs_windowSize + 1; // (fSize / doubleSize) is the number of values in the file //printf("____%d____", nbWindows); descriptor->histogram = malloc(sizeof(*descriptor->histogram) * nbWindows); for(iInterval = 0; iInterval < globs_nbInterval; iInterval++) { //=========================================================================================================================================================================================================================================================================================================================== //////////////////////////////////// NEEEEEEEEDDDDDDDDDDDSSSSSSSSSSSS TOOOOOOOOO BEEEEEEEEEEE EXPLAINEDDDDDDDDDD intervalsThreshold[iInterval] = ((iInterval + 1) / (double)globs_nbInterval) * (globs_maxFrequency - globs_minFrequency) + globs_minFrequency; //printf("\nintervalsThreshold : %f", intervalsThreshold[iInterval]); } // Initialise the histogram with zeros for(iWindow = 0; iWindow < nbWindows; iWindow++) { descriptor->histogram[iWindow] = malloc(sizeof(double) * globs_nbInterval); for(iInterval = 0; iInterval < globs_nbInterval; iInterval++) descriptor->histogram[iWindow][iInterval] = 0; } fflush(stdout); //printf("\nglobs_windowSize : %d globs_nbInterval : %d, globs_maxFrequency : %f globs_minFrequency : %f", globs_windowSize, globs_nbInterval, globs_maxFrequency, globs_minFrequency); iElem = iWindow = iInterval = 0; while(readStruct(file, &tmp, doubleSize)) { if(iElem == globs_windowSize) { if(iInterval < globs_nbInterval) //printf("\niWindow : %d, iElem : %d histo : %d", iWindow, iElem, descriptor->histogram[iWindow][iInterval]); fflush(stdout); iElem = 0; iWindow++; } *tmp = endianSwap_double(*tmp); if(*tmp <= intervalsThreshold[0]) (descriptor->histogram[iWindow][0])++; else { for(iInterval = 0; iInterval < globs_nbInterval; iInterval++) { if(*tmp <= intervalsThreshold[iInterval]) { (descriptor->histogram[iWindow][iInterval])++; //printf("d_______%d", iInterval); break; } } if(*tmp > intervalsThreshold[globs_nbInterval - 1]) (descriptor->histogram[iWindow][globs_nbInterval - 1])++; } iElem++; } free(tmp); free(intervalsThreshold); return descriptor; }
int main (void) { int i = 0,j =0, k =0,birthdayB = 0, eventB =0; int linesB = 0, linesE = 0; struct file* tabl1; FILE* f1; FILE* f2; FILE* f3; char name[20], sureName[20]; char *a, *b,*cc,*M, *names,*namess, *sureNames,*event,*task[50], *help[200]; int *c , * year, *ID, *day, *month,*hours,*minutes; char *comands[50]= { " ", "create",// 1 "update",// 2 "delete",// 3 "show",// 4 "sort",// 5 "count",// 6 "help",// 7 "exit", // 8 }; char *showTo[]= { " ", "birthday", // 1 "event",// 2 "delete",// 3 "name",// 4 "sureName",// 5 "ID",// 6 "calendar",// 7 "task",// 8 "date",// 9 "month",// 10 "year",// 11 "all",// 12 }; char *monthsss[13]= { " ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; do{ linesB = 0; char str [50]; do { gets(str); printf ("Use the help menu\n"); }while (strlen (str ) < 1); char sep [10]=":. "; char *istr; a = strtok (str,sep); b = strtok (NULL,sep); for (i=1;i<9;i++){ if (strcmp(a,comands[i]) == 0) k = i; } if (k == 1){ for (i=0;i<10;i++){ if (strcmp(b,showTo[i]) == 0) k = i; } if (k == 1){ ID = (int *)atoi(strtok (NULL,sep)); printf ("%d ", ID ); day = (int *)atoi(strtok (NULL,sep)); printf ("%d ", day ); month =(int *)atoi(strtok (NULL,sep)); printf ("%d ",month ); year = (int *)atoi(strtok (NULL,sep)); printf ("%d ", year ); names = strtok (NULL,sep); printf ("%s ", names ); sureNames = strtok (NULL,sep); printf ("%s ", sureNames ); f1 = fopen ("birthday.txt", "a+"); fprintf(f1, "%d %d %d %d %s %s \n",ID, day, month, year, names, sureNames); fclose(f1); } if (k == 2){ // event Created event ID 1 : 25.01.2016 14:00 "IT Academy" ID = (int *)atoi(strtok (NULL,sep)); printf ("%d ", ID ); day = (int *)atoi(strtok (NULL,sep)); printf ("%d ", day ); month =(int *)atoi(strtok (NULL,sep)); printf ("%d ", month ); year =(int *) atoi(strtok (NULL,sep)); printf ("%d ", year ); hours =(int *)atoi(strtok (NULL,sep)); printf ("%d ", hours ); minutes =(int *) atoi(strtok (NULL,sep)); printf ("%d ", minutes ); char sep [10]=""""; event = strtok (NULL,sep); printf ("%s ", event); f2 = fopen ("event.txt", "a+"); fprintf(f2, "%d %d %d %d %d %d %s \n",ID, day, month, year, hours,minutes, event); fclose(f2); } if (k == 8){ // task ID =(int *) atoi(strtok (NULL,sep)); printf ("%d ", ID ); day = (int *)atoi(strtok (NULL,sep)); printf ("%d ", day ); month =(int *)atoi(strtok (NULL,sep)); printf ("%d ", month ); year =(int *) atoi(strtok (NULL,sep)); printf ("%d ", year ); char sep [10]=""; task[100] = strtok (NULL,sep); printf ("%s ", task[50]); f3 = fopen ("task.txt", "a+"); fprintf(f3, "%d %d %d %d %s \n",ID, day, month, year, task[100]); fclose(f3); } k=0; linesB =0; } if (k == 2) { // update ID 10 name printf ("ok1"); for (i=1;i<7;i++){ if (strcmp(b,showTo[i]) == 0) k = i; } if (k == 4){ // update name yyyy yyyyyy names = strtok (NULL,sep); printf ("%s\n",names); namess = strtok (NULL,sep); printf ("%s\n",namess); f1 = fopen("birthday.txt","r"); while (!feof(f1)){ if (fgetc(f1) == '\n') linesB++; } fclose (f1); tabl1 = (struct file*)calloc(linesB, sizeof(struct file)); readStruct("birthday.txt",tabl1,linesB); printf ("%d",linesB); for (i=0; i<linesB; i++){ printf ("%s\n",tabl1[i].name); if (strcmp(tabl1[i].name,names) == 0){ printf ("ZNAISHLO\n"); strcpy(tabl1[i].name, namess); } } for (i=0; i<linesB; i++){ printf("%d %d %d %d %s %s\n",tabl1[i].ID, tabl1[i].day, tabl1[i].month, tabl1[i].year, tabl1[i].name, tabl1[i].sureName); } printf ("linesB = %d\n",linesB); f1 = fopen("birthday.txt","w"); fclose(f1); f1 = fopen("birthday.txt","a+"); for (i=0; i<linesB; i++){ fprintf(f1, "%d %d %d %d %s %s\n",tabl1[i].ID, tabl1[i].day, tabl1[i].month, tabl1[i].year, tabl1[i].name, tabl1[i].sureName); } fclose(f1); k=0; } if (k == 6) { c =(int *) atoi(strtok (NULL,sep)); names = strtok (NULL,sep); namess = strtok (NULL,sep); printf ("ok1namess = %s", namess ); for (j=1;j<7;j++){ if (strcmp(names,showTo[j]) == 0) k = j; } f1 = fopen("birthday.txt","r"); while (!feof(f1)){ if (fgetc(f1) == '\n') linesB++; } fclose (f1); tabl1 = (struct file*)calloc(linesB, sizeof(struct file)); readStruct("birthday.txt",tabl1,linesB); for (i=0; i<linesB; i++){ if (tabl1[i].ID == (int)c){ if ( k == 4){ strcpy(tabl1[i].name, namess); } else { strcpy(tabl1[i].sureName, namess); } } } f1 = fopen("birthday.txt","w"); fclose(f1); f1 = fopen("birthday.txt","a+"); for (i=0; i<linesB; i++){ fprintf(f1, "%d %d %d %d %s %s\n",tabl1[i].ID, tabl1[i].day, tabl1[i].month, tabl1[i].year, tabl1[i].name, tabl1[i].sureName); } fclose(f1); free ( tabl1 ); } k=0; linesB =0; } if (k == 3){ for (i=1;i<7;i++){ if (strcmp(b,showTo[i]) == 0) k = i; } if (k == 1) { c =(int *)atoi(strtok (NULL,sep)); f1 = fopen("birthday.txt","r"); while (!feof(f1)){ if (fgetc(f1) == '\n') linesB++; } fclose (f1); tabl1 = (struct file*)calloc(linesB, sizeof(struct file)); readStruct("birthday.txt",tabl1,linesB); for (i=0; i<linesB; i++){ if ((int)c == tabl1[i].ID) { f3 = fopen ("deleteB.txt", "a+"); fprintf(f3, "%d %d %d %d %s %s\n",tabl1[i].ID, tabl1[i].day, tabl1[i].month, tabl1[i].year, tabl1[i].name, tabl1[i].sureName); fclose(f3); } } f1 = fopen("birthday.txt","w"); fclose(f1); for (i=0; i<linesB; i++){ if ((int)c == tabl1[i].ID) break; else { f1 = fopen("birthday.txt","a+"); fprintf(f1, "%d %d %d %d %s %s\n",tabl1[i].ID, tabl1[i].day, tabl1[i].month, tabl1[i].year, tabl1[i].name, tabl1[i].sureName); fclose(f1); free ( tabl1 ); linesB =0; } } } k = 0; linesB =0; } if (k == 4){ for (i=1;i<9;i++){ if (strcmp(b,showTo[i]) == 0) k = i; } // printf ("k = %d", k); if(k == 1){ b = strtok (NULL,sep); // printf ("b = %s\n", b); for (i=1;i<14;i++){ if (strcmp(b,showTo[i]) == 0) k = i; } // printf ("k = %d", k); if (k == 10) // show birthday month December { int m; b = strtok (NULL,sep); printf ("Comand = %s\n",b); // M = strtok (NULL,sep); // printf ("M = %s\n",M); for (i=0;i<13;i++){ if (strcmp(b,monthsss[i]) == 0){ m = i; printf ("m = %d\n",m); } } f1 = fopen("birthday.txt","r"); while (!feof(f1)){ if (fgetc(f1) == '\n') linesB++; } fclose (f1); tabl1 = (struct file*)calloc(linesB, sizeof(struct file)); readStruct("birthday.txt",tabl1,linesB); for (i=0; i<linesB; i++){ for (j=0; j < linesB; j++ ) { if (tabl1[i].ID < tabl1[j].ID) { struct file buff; buff = tabl1[i]; tabl1[i] = tabl1[j]; tabl1[j] = buff; } } } for(i=0; i<linesB; i++){ if (m == tabl1[i].month ) printf ("%d %d %d %d %s %s\n",tabl1[i].ID, tabl1[i].day, tabl1[i].month, tabl1[i].year, tabl1[i].name, tabl1[i].sureName); } fclose(f1); free ( tabl1 ); } if (k == 12){ f1 = fopen ("birthday.txt", "r"); // виведення на екран вссіх ударів while (fscanf(f1, "%d %d %d %d %s %s\n", &ID, &day, &month, &year, name, sureName) != EOF) printf ("%d %d %d %d %s %s\n",ID, day, month, year, name, sureName); fclose(f1); k=0; } } if (k==2){// fprintf(f2, "%d %d %d %d %d %d %s \n",ID, day, month, year, hours,minutes, event); f1 = fopen ("event.txt", "r"); // виведення на екран вссіх ударів do{ //(fscanf(f2, "%d %d %d %d %d %d", &ID, &day, &month, &year, &hours,&minutes)!=EOF) fscanf(f1, "%d %d %d %d %d %d", &ID, &day, &month, &year, &hours,&minutes); fgets(event, 25, f1); printf ("%d %d.%d.%d %d:%d %s",ID, day, month, year, hours,minutes, event); }while (fscanf(f1, "%d %d %d %d %d %d", &ID, &day, &month, &year, &hours,&minutes) != EOF); fclose(f1); k=0; } if (k == 8){// %d %d %d %d %s \n",ID, day, month, year, task[50]) f1 = fopen ("task.txt", "r"); // виведення на екран вссіх ударів do{ fscanf(f1, "%d %d %d %d", &ID, &day, &month, &year ); fgets(event, 30, f1); printf ("%d %d.%d.%d %s",ID, day, month, year, event); }while (fscanf(f1, "%d %d %d %d %d %d", &ID, &day, &month, &year) != EOF); fclose(f1); k=0; } if (k == 3) { f1 = fopen ("deleteB.txt", "r"); // виведення на екран вссіх ударів while (fscanf(f1, " %d %d %d %d %s %s\n", &ID, &day, &month, &year, name, sureName) != EOF) printf (" %d %d %d %d %s %s\n",ID, day, month, year, name, sureName); fclose(f1); k=0; } if (k == 7){ int year , daycode, leapyear, m; { year = (int ) atoi(strtok (NULL,sep)); M = strtok (NULL,sep); for (i=1;i<13;i++){ if (strcmp(M,monthsss[i]) == 0){ m = i; printf ("m = %d",m); } } SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 7); daycode = determinedaycode(year); determineleapyear(year); calendar(year, daycode, m); { f1 = fopen("birthday.txt","r"); while (!feof(f1)){ if (fgetc(f1) == '\n') linesB++; } fclose (f1); tabl1 = (struct file*)calloc(linesB, sizeof(struct file)); readStruct("birthday.txt",tabl1,linesB); for (i=0; i<linesB; i++){ if ((tabl1[i].year == year ) && (tabl1[i].month == m)) printf ("%d %d %d %d %s %s\n",tabl1[i].ID, tabl1[i].day, tabl1[i].month, tabl1[i].year, tabl1[i].name, tabl1[i].sureName); } } free ( tabl1 ); k = 0; printf("\n"); k=0; } } k=0; linesB =0; } if ( k==5 ){ linesB = 0; for (i=1;i<3;i++){ if (strcmp(b,showTo[i]) == 0) k = i; } if(k == 1){ { f1 = fopen("birthday.txt","r"); while (!feof(f1)) if (fgetc(f1) == '\n') linesB++; } fclose (f1); tabl1 = (struct file*)calloc(linesB, sizeof(struct file)); readStruct("birthday.txt",tabl1,linesB); for (i=0; i<linesB; i++){ for (j=0; j < linesB; j++ ) { if (strcmp (tabl1[i].name, tabl1[j].name) < 0) { struct file buff; buff = tabl1[i]; tabl1[i] = tabl1[j]; tabl1[j] = buff; } } } for(i=0; i<linesB; i++){ printf ("%d %d %d %d %s %s\n",tabl1[i].ID, tabl1[i].day, tabl1[i].month, tabl1[i].year, tabl1[i].name, tabl1[i].sureName); } fclose(f1); free ( tabl1 ); } if (k == 2){ linesB = 0; { f2 = fopen("event.txt","r"); while (!feof(f2)) if (fgetc(f1) == '\n') linesB++; } fclose (f2); tabl1 = (struct file*)calloc(linesB, sizeof(struct file)); readStructE("event.txt",tabl1,linesB); for (i=0; i<linesB; i++){ for (j=0; j < linesB; j++ ) { if (tabl1[i].ID < tabl1[j].ID) { struct file buff; buff = tabl1[i]; tabl1[i] = tabl1[j]; tabl1[j] = buff; } } } for(i=0; i<linesB; i++){//%d %d %d %d %d %d %s", &ID, &day, &month, &year, &hours,&minutes, event printf ("%d %d %d %d %d %d %s",tabl1[i].ID, tabl1[i].day, tabl1[i].month, tabl1[i].year, tabl1[i].hours,tabl1[i].minutes, tabl1[i].task); } fclose(f2); free ( tabl1 ); } k=0; } if (k == 6){ for (i=1;i<9;i++){ if (strcmp(b,showTo[i]) == 0) k = i; } int m; if (k == 1) { cc = strtok (NULL,sep); for (i=1;i<14;i++){ if (strcmp(cc,showTo[i]) == 0) k = i; } if (k == 10) { // "month" M = strtok (NULL,sep); for (i=0;i<13;i++){ if (strcmp(M,monthsss[i]) == 0){ m = i; } } f1 = fopen ("birthday.txt", "r"); while (fscanf(f1, "%d %d %d %d %s %s\n", &ID, &day, &month, &year, name, sureName) != EOF){ if (m == (int)month){ birthdayB++; printf ("%d %d %d %d %s %s\n", ID, day, month, year, name, sureName); } } printf ("\nbirthday %s = %d\n",M, birthdayB); fclose(f1); k=0; birthdayB = 0; } if (k == 11) { //"year" birthdayB = 0; printf("ok3"); c = (int *)atoi(strtok (NULL,sep)); printf ("c= %d bir = %d\n",c,birthdayB); f1 = fopen ("birthday.txt", "r"); // виведення на екран вссіх ударів while (fscanf(f1, "%d %d %d %d %s %s\n", &ID, &day, &month, &year, name, sureName) != EOF){ // printf ("c = %d year = %d\n", c , year); if (c == year){ birthdayB++; } } printf ("\nKKKKKbirthday %d = %d\n",c, birthdayB); fclose(f1); k=0; } if (k == 12){ f1 = fopen("birthday.txt","r"); while (!feof(f1)){ if (fgetc(f1) == '\n') birthdayB++; }while (!EOF); fclose(f1); printf ("ebirthday = %d\n",birthdayB); } } if (k == 2) { f2 = fopen("event.txt","r"); while (!feof(f2)){ if (fgetc(f2) == '\n') eventB++; } while (!EOF); fclose(f2); printf ("event = %d\n",eventB ); } if (k == 8) { f3 = fopen("task.txt","r"); while (!feof(f3)) { if (fgetc(f3) == '\n') eventB++; } while (!EOF); fclose(f3); printf ("task = %d\n",eventB); } birthdayB = 0; eventB = 0; k=0; } if (k == 7 ){ f1 = fopen ("comand.txt", "r"); // виведення на екран вссіх ударів /* while (!feof(f1)){ fgets(l, 100, f1); printf ("%s",l);*/ while (!feof(f1)) { // fscanf(f1, "%d %d %d %d", &ID, &day, &month, &year ); fgets(help, 200, f1); printf ("%s",help); } //(fscanf(f1, "%d %d %d %d %d %d", &ID, &day, &month, &year) != EOF); printf ("\n"); fclose(f1); k=0; } }while( k != 8); }
bool WAV::Decode (Resource *resource, AudioBuffer *audioBuffer) { WAVE_Format wave_format; RIFF_Header riff_header; WAVE_Data wave_data; unsigned char* data; FILE_HANDLE *file = NULL; if (resource->path) { file = lime::fopen (resource->path, "rb"); if (!file) { return false; } int result = lime::fread (&riff_header, sizeof (RIFF_Header), 1, file); if ((riff_header.chunkID[0] != 'R' || riff_header.chunkID[1] != 'I' || riff_header.chunkID[2] != 'F' || riff_header.chunkID[3] != 'F') || (riff_header.format[0] != 'W' || riff_header.format[1] != 'A' || riff_header.format[2] != 'V' || riff_header.format[3] != 'E')) { lime::fclose (file); return false; } long int currentHead = 0; bool foundFormat = false; while (!foundFormat) { currentHead = lime::ftell (file); result = lime::fread (&wave_format, sizeof (WAVE_Format), 1, file); if (result != 1) { LOG_SOUND ("Invalid Wave Format!\n"); lime::fclose (file); return false; } if (wave_format.subChunkID[0] != 'f' || wave_format.subChunkID[1] != 'm' || wave_format.subChunkID[2] != 't' || wave_format.subChunkID[3] != ' ') { lime::fseek (file, wave_format.subChunkSize, SEEK_CUR); } else { foundFormat = true; } } bool foundData = false; while (!foundData) { currentHead = lime::ftell (file); result = lime::fread (&wave_data, sizeof (WAVE_Data), 1, file); if (result != 1) { LOG_SOUND ("Invalid Wav Data Header!\n"); lime::fclose (file); return false; } if (wave_data.subChunkID[0] != 'd' || wave_data.subChunkID[1] != 'a' || wave_data.subChunkID[2] != 't' || wave_data.subChunkID[3] != 'a') { lime::fseek (file, currentHead + sizeof (WAVE_Data) + wave_data.subChunkSize, SEEK_SET); } else { foundData = true; } } audioBuffer->data = new Bytes (wave_data.subChunkSize); audioBuffer->length = wave_data.subChunkSize; if (!lime::fread (audioBuffer->data->Data (), wave_data.subChunkSize, 1, file)) { LOG_SOUND ("error loading WAVE data into struct!\n"); lime::fclose (file); return false; } lime::fclose (file); } else { const char* start = (const char*)resource->data->Data (); const char* end = start + resource->data->Length (); const char* ptr = start; memcpy (&riff_header, ptr, sizeof (RIFF_Header)); ptr += sizeof (RIFF_Header); if ((riff_header.chunkID[0] != 'R' || riff_header.chunkID[1] != 'I' || riff_header.chunkID[2] != 'F' || riff_header.chunkID[3] != 'F') || (riff_header.format[0] != 'W' || riff_header.format[1] != 'A' || riff_header.format[2] != 'V' || riff_header.format[3] != 'E')) { return false; } ptr = find_chunk (ptr, end, "fmt "); if (!ptr) { return false; } readStruct (wave_format, ptr); if (wave_format.subChunkID[0] != 'f' || wave_format.subChunkID[1] != 'm' || wave_format.subChunkID[2] != 't' || wave_format.subChunkID[3] != ' ') { LOG_SOUND ("Invalid Wave Format!\n"); return false; } ptr = find_chunk (ptr, end, "data"); if (!ptr) { return false; } const char* base = readStruct (wave_data, ptr); if (wave_data.subChunkID[0] != 'd' || wave_data.subChunkID[1] != 'a' || wave_data.subChunkID[2] != 't' || wave_data.subChunkID[3] != 'a') { LOG_SOUND ("Invalid Wav Data Header!\n"); return false; } audioBuffer->data->Resize (wave_data.subChunkSize); size_t size = wave_data.subChunkSize; if (size > (end - base)) { return false; } unsigned char* bytes = audioBuffer->data->Data (); memcpy (bytes, base, size); } audioBuffer->sampleRate = (int)wave_format.sampleRate; audioBuffer->channels = wave_format.numChannels; audioBuffer->bitsPerSample = wave_format.bitsPerSample; return true; }
bool loadWavSampleFromBytes(const float *inData, int len, QuickVec<unsigned char> &outBuffer, int *channels, int *bitsPerSample, int* outSampleRate) { const char* start = (const char*)inData; const char* end = start + len; const char* ptr = start; WAVE_Format wave_format; RIFF_Header riff_header; WAVE_Data wave_data; unsigned char* data; // Read in the first chunk into the struct memcpy(&riff_header, ptr, sizeof(RIFF_Header)); ptr += sizeof(RIFF_Header); //check for RIFF and WAVE tag in memeory if ((riff_header.chunkID[0] != 'R' || riff_header.chunkID[1] != 'I' || riff_header.chunkID[2] != 'F' || riff_header.chunkID[3] != 'F') || (riff_header.format[0] != 'W' || riff_header.format[1] != 'A' || riff_header.format[2] != 'V' || riff_header.format[3] != 'E')) { LOG_SOUND("Invalid RIFF or WAVE Header!\n"); return false; } //Read in the 2nd chunk for the wave info ptr = find_chunk(ptr, end, "fmt "); if (!ptr) { return false; } readStruct(wave_format, ptr); //check for fmt tag in memory if (wave_format.subChunkID[0] != 'f' || wave_format.subChunkID[1] != 'm' || wave_format.subChunkID[2] != 't' || wave_format.subChunkID[3] != ' ') { LOG_SOUND("Invalid Wave Format!\n"); return false; } ptr = find_chunk(ptr, end, "data"); if (!ptr) { return false; } const char* base = readStruct(wave_data, ptr); //check for data tag in memory if (wave_data.subChunkID[0] != 'd' || wave_data.subChunkID[1] != 'a' || wave_data.subChunkID[2] != 't' || wave_data.subChunkID[3] != 'a') { LOG_SOUND("Invalid Wav Data Header!\n"); return false; } //Allocate memory for data //data = new unsigned char[wave_data.subChunk2Size]; // Read in the sound data into the soundData variable size_t size = wave_data.subChunkSize; if (size > (end - base)) { return false; } /*mlChannels = wave_format.numChannels; if (mlChannels == 2) { if (wave_format.bitsPerSample == 8) { mFormat = AL_FORMAT_STEREO8; mlSamples = size / 2; } else //if (wave_format.bitsPerSample == 16) { mlSamples = size / 4; mFormat = AL_FORMAT_STEREO16; } } else //if (mlChannels == 1) { if (wave_format.bitsPerSample == 8) { mlSamples = size; mFormat = AL_FORMAT_MONO8; } else //if (wave_format.bitsPerSample == 16) { mlSamples = size / 2; mFormat = AL_FORMAT_MONO16; } } mlFrequency = wave_format.sampleRate; mfTotalTime = float(mlSamples) / float(mlFrequency);*/ //Store in the outbuffer outBuffer.Set((unsigned char*)base, size); //Now we set the variables that we passed in with the //data from the structs *outSampleRate = (int)wave_format.sampleRate; //The format is worked out by looking at the number of //channels and the bits per sample. *channels = wave_format.numChannels; *bitsPerSample = wave_format.bitsPerSample; //clean up and return true if successful //fclose(f); //delete[] data; return true; }