Exemple #1
0
int main(int argc, char const* const argv[]) {

    check_argv(argc, argv);

    std::string mode(argv[1]);

    if(mode == "encode") {
        return encode_to_file(create_test_m09(), "test09.c2ml.data");
    }
    else if(mode == "decode") {
        M09 m09;
        validate_decode(m09, "test09.ml2c.data");
    }
    else {
        std::cerr << "Invalid second argument: "
                  << argv[1]
                  << std::endl;
        return 1;
    }
}
void *complete_encode_worker(void* arg)
{
	int ret;
	ENC_WRK_ARGS *args = (ENC_WRK_ARGS*)arg; // parse argument struct

	while (true) {
#ifdef __VERBOSE_
		cout << "Checking for work\n";
#endif
		// determine which file to process next
		bool bFoundWork = false;
		int iFileIdx = -1;

		pthread_mutex_lock(&mutFilesFinished);
		for (int i = 0; i < args->iNumFiles; i++) {
			if (!args->pbFilesFinished[i]) {
				args->pbFilesFinished[i] = true; // mark as being worked on
				iFileIdx = i;
				bFoundWork = true;
				break;
			}
		}
		pthread_mutex_unlock(&mutFilesFinished);

		if (!bFoundWork) {// done yet?
			return NULL; // break
		}
		string sMyFile = args->pFilenames->at(iFileIdx);
		string sMyFileOut = sMyFile.substr(0, sMyFile.length() - 3) + "mp3";

		// start working
		FMT_DATA *hdr = NULL;
		short *leftPcm = NULL, *rightPcm = NULL;
		// init encoding params
		lame_global_flags *gfp = lame_init();
		lame_set_brate(gfp, 192); // increase bitrate
		lame_set_quality(gfp, 3); // increase quality level
		lame_set_bWriteVbrTag(gfp, 0);

		// parse wave file
#ifdef __VERBOSE_
		printf("Parsing %s ...\n", sMyFile.c_str());
#endif
		int iDataSize = -1;
		ret = read_wave(sMyFile.c_str(), hdr, leftPcm, rightPcm, iDataSize);
		if (ret != EXIT_SUCCESS) {
			printf("Error in file %s. Skipping.\n", sMyFile.c_str());
			continue; // see if there's more to do
		}

		lame_set_num_channels(gfp, hdr->wChannels);
		lame_set_num_samples(gfp, iDataSize / hdr->wBlockAlign);
		// check params
		ret = lame_init_params(gfp);
		if (ret != 0) {
			cerr << "Invalid encoding parameters! Skipping file." << endl;
			continue;
		}

		// encode to mp3
		ret = encode_to_file(gfp, hdr, leftPcm, rightPcm, iDataSize, sMyFileOut.c_str());
		if (ret != EXIT_SUCCESS) {
			cerr << "Unable to encode mp3: " << sMyFileOut.c_str() << endl;
			continue;
		}

		printf("[:%i][ok] .... %s\n", args->iThreadId, sMyFile.c_str());
		++args->iProcessedFiles;

		lame_close(gfp);
		if (leftPcm != NULL) delete[] leftPcm;
		if (rightPcm != NULL) delete[] rightPcm;
		if (hdr != NULL) delete hdr;
	}

	pthread_exit((void*)0);
}