Passing wav file samples through socket in c -
i'm (unsuccessfully) trying pass .wav file through socket in c.
the following code reads .wav file , assigns short samples variable (num_samples being size).
char* filename = "./test.wav"; file* f; short* samples; // stored signal int num_samples, curr_samples; // count of signal samples if ((f = fopen(filename, "rb")) == null) { fprintf(stderr, "cannot open %s\n", filename); return; } /* reads .wav file memory (samples) */ if (read_wav(f, &samples, &num_samples) < 0) { return; } fclose(f);
then, samples loaded iteratively buffers , passed through socket
int buffer_size = 320; unsigned char buffer[buffer_size]; short buffersamples[buffer_size/2]; int curr_samples = 0; while(curr_samples < num_samples) { bzero(buffer,buffer_size); bzero(buffersamples,buffer_size/2); // store samples in short array (i = curr_samples; < buffer_size/2 + curr_samples; i++) { buffersamples[i-curr_samples] = *(samples + i); } // assign buffer (i = 0; < buffer_size; i+=2) { unsigned char upper = buffersamples[i/2] & 0xff; unsigned char lower = buffersamples[i/2] >> 8; buffer[i] = lower; buffer[i+1] = upper; } n = write(sockfd,buffer,strlen(buffer)); if (n < 0) error("error writing socket"); // sleep , increment usleep(10000); curr_samples += buffer_size/2; }
for sake of simplicity, have not posted entire code (socket definitions etc)
i believe have confirmed buffersamples correctly stores signal in shorts each iteration (by comparing prints output of command "od -s test.wav"), suspect problem occurs when assign short array char buffer. have tried altering endianess no avail.
the server reads input client have no access to, handles inputs correctly, problem lies in client.
i have little experience sockets , byte conversions, grateful if provided me insight. solution quite obvious experienced.
edit: turns out, problem lies in interface used pass signal code. so, apart strlen() function, seems ok after all. nevertheless, thank educating tips!
mostly looks ok. code tidier, more functions convert between short , 2x char etc... improve readability, have written looks correct @ glance.
there 1 problem however.
n = write(sockfd,buffer,strlen(buffer));
strlen()
counting length of null terminated string.
if buffer has byte 0, strlen
stop counting. want send size of buffer each time sizeof(buffer)
Comments
Post a Comment