// mpcenc2.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include "libmpcenc.h" #define DUMP(fn, buf, len) { FILE *f = fopen(fn, "wb"); fwrite(buf, 1, len, f); fclose(f); } #define WRITE_PACKET(type) { mpc_encoder_get_hdr_packet(encoder, type, buf, &len); fwrite(buf, 1, len, outfile); } // Globals mpc_encoder_t encoder; mpc_encoder_config_t *config; FILE *outfile; unsigned char *buf = NULL; void write_head() { int len; WRITE_PACKET(PACKET_MAGIC) WRITE_PACKET(PACKET_TYPE('S','H')) WRITE_PACKET(PACKET_TYPE('R','G')) WRITE_PACKET(PACKET_TYPE('E','I')) } void write_tail() { int len; // STREAM END WRITE_PACKET(PACKET_TYPE('S','E')) } void encode_noise_frame() { // generate random data float pcm[1152 * 2]; int len = 0; for (int i=0; i<1152*2; i++) { pcm[i] = ((rand()%65536) - 32768) / 70000.0; } mpc_encoder_encode_pcm(encoder, pcm, 1152*2, buf, &len); if (len > 0) { fwrite(buf, 1, len, outfile); } } int test_enc() { int ret; mpc_encoder_config(encoder, 44100, 2, 5); mpc_encoder_get_config(encoder, &config); mpc_encoder_init(encoder); buf = (unsigned char*)malloc(config->buffer_size); write_head(); // let's encode several dummy frames for (int i=0; i<150; i++) { encode_noise_frame(); } write_tail(); return 0; } int _tmain(int argc, _TCHAR* argv[]) { int ret; // create encoder instance ret = mpc_encoder_open(&encoder); if (ret < 0) { printf("failed to create instance\n"); return -1; } outfile = fopen("output.mpc", "wb"); if (!outfile) { mpc_encoder_close(encoder); printf("Cannot open file\n"); return -1; } ret = test_enc(); mpc_encoder_close(encoder); if (buf) free(buf); fclose(outfile); return ret; }