Created
September 19, 2017 07:14
-
-
Save andre-dubber/d7040ad6fd2b681f9a8db4201ac3c695 to your computer and use it in GitHub Desktop.
CNODE receiving part
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ETERM *process_block_encoding(ETERM *tuplep, ETERM *method_param, lame_t lame_config, int offset) { | |
ETERM *action, *resp; | |
char *filename; | |
short int input_buffer[EXCHANGE_BUFSIZE/2]; //Buffer to be used passing to lame | |
uint8_t bytes_buffer[EXCHANGE_BUFSIZE]; //Buffer received from Elixir | |
unsigned char output_buffer[EXCHANGE_BUFSIZE/2]; //Output buffer from lame | |
int part_no = offset/EXCHANGE_BUFSIZE; //Only used to identify call number to this block | |
if (part_no > 5) part_no = 5; //Stop condition to stop writing log files | |
char part_ofilename[100]; //MP3 output file | |
char part_encfilename[100]; //Prior to "compressing" input | |
char part_ifilename[100]; //After "compressing" to int input | |
sprintf(part_ifilename, "/tmp/elixir_i%i.wav", part_no); | |
sprintf(part_ofilename, "/tmp/elixir_o%i.mp3", part_no); | |
sprintf(part_encfilename, "/tmp/elixir_enc%i.mp3", part_no); | |
int len, encoded_len; void *ptr; | |
int write; | |
FILE *mp3_ipartial = fopen(part_ifilename, "wb"); | |
FILE *mp3_opartial = fopen(part_ofilename, "wb"); | |
FILE *mp3_encpartial = fopen(part_encfilename, "wb"); | |
action = erl_element(1, tuplep); | |
if (strncmp(ERL_ATOM_PTR(action), "encode", 6) == 0) { | |
len = ERL_BIN_SIZE(method_param); | |
ptr = ERL_BIN_PTR(method_param); | |
memset(input_buffer, '\0', sizeof(input_buffer)); | |
memset(bytes_buffer, '\0', sizeof(bytes_buffer)); | |
memcpy(bytes_buffer, ptr, len); //Copying Elixir data into local array | |
//fprintf(stdout, "Data sizes, received %i short int: %i", sizeof(bytes_buffer[0]), sizeof(short int)); | |
int i; | |
for (i = 0; i < len; i += 2) | |
input_buffer[i/2] = bytes_buffer[i] | (short int)bytes_buffer[i+1] << 8; //little endian | |
if ( memcmp(input_buffer, bytes_buffer, len)) { //Just checking if memory remained the same after "compressing" | |
fprintf(stderr, "\n Arrays match after casting "); | |
} else { | |
fprintf(stderr, "\n Arrays DONT match after casting "); | |
} | |
write = fwrite(bytes_buffer, len, 1, mp3_ipartial); //Writing buffer from Elixir to file | |
write = fwrite(input_buffer, len/2, 1, mp3_encpartial); | |
//fprintf(stdout, "\nBlock received %i bytes", len); | |
encoded_len = lame_encode_buffer(lame_config, input_buffer, input_buffer, len, output_buffer, len); | |
//fprintf(stdout, "\n Encoded block, result size %i bytes", encoded_len); | |
write = fwrite(output_buffer, encoded_len, 1, mp3_opartial); //Saving encoded lame block into file | |
resp = erl_format("{cnode, ~w}", erl_mk_binary(output_buffer, encoded_len)); | |
} | |
erl_free_term(action); | |
fclose(mp3_opartial); | |
fclose(mp3_encpartial); | |
fclose(mp3_ipartial); | |
return resp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment