Esempio n. 1
0
/* This is the function that is called when the program starts. */
int main (int argc, char *argv[])
{
  /* Variable declarations. */
  struct wavpcm_input input;
  struct wavpcm_output output;
  short buffer[BUFFERSIZE]; 
  short reconstructedBuffer[BUFFERSIZE];
  int bufPos, bufIndex, read;

  struct fade_chunk chunk;
  chunk.fadeOutCoefficient = FADEOUTCOEFFICIENT;
  printf("chunk.fadeOutCoefficient: %f\n",chunk.fadeOutCoefficient);
  chunk.currentFadeCoefficient = 1;

  memset(&input, 0, sizeof(struct wavpcm_input));
  input.resource=INPUTWAVFILE;
  memset(&output, 0, sizeof(struct wavpcm_output));
  output.resource=OUTPUTWAVFILE;

  /* First open input file and parse header, */
  wavpcm_input_open (&input);
  /* and then use this same header configuration for the output file */
  wavpcm_output_copy_settings(&input, &output);
  wavpcm_output_open(&output);
  
  /*bufPos expressed in temporal samples*/
  for (bufPos=0; bufPos<input.samplesAvailable; bufPos+=(BUFFERSIZE/2)) {
    /* Try to read BUFFERSIZE samples (16 bits, pairwise identical if input is mono, interleaved if input is stereo)  */
    /* into buffer, with read the actual amount read (expressed in bytes! =  (2*read)/(channels * bitDepth/8) array elements)*/
    read = wavpcm_input_read (&input, buffer);

    /* transform buffer */
    if(bufPos<(2*(BUFFERSIZE/2)))
      {
      printf("chunk.fadeOutCoefficient: %f\n",chunk.fadeOutCoefficient);
      printf("chunk.currentFadeCoefficient: %f\n",chunk.currentFadeCoefficient);
      }

    fadeOut(buffer,&chunk);

    /* if required, dump compressed output */

    /* inverse transform buffer */
    for (bufIndex=0; bufIndex<BUFFERSIZE; bufIndex++)
      reconstructedBuffer[bufIndex]=buffer[bufIndex];

    /* dump reconstructed output */
    wavpcm_output_write (&output, reconstructedBuffer, read);
  }

  /* finalize output (write header) and close */
  wavpcm_output_close (&output);  
  /* Return successful exit code. */
  return 0;
}
Esempio n. 2
0
/* This is the function that is called when the program starts. */
int main(int argc, char *argv[]){
	int bufPos, read;

	//initialization
	memset(&input, 0, sizeof(struct wavpcm_input));
	input.resource = INPUTWAVFILE;
	wavpcm_input_open(&input);
	memset(&output, 0, sizeof(struct wavpcm_output));
	output.resource = OUTPUTWAVFILE;
	wavpcm_output_copy_settings(&input, &output);
	wavpcm_output_open(&output);

	memset(&historyChunkAnalysis, 0, sizeof(struct chunk));
	memset(&historyChunkSynthesis, 0, sizeof(struct chunk));
	memset(largeCryptoBuffer, 0, sizeof(largeCryptoBuffer));
	memset(wavbuffer, 0, sizeof(wavbuffer));
	
#ifdef CRYPTO
	//Create RSA ctx master & slave
	calculate_parameters_RSA(&RSA_ctx_master);
	calculate_parameters_RSA(&RSA_ctx_slave);

	//Create ENC ctx master & slave
	ENC_ctx_master.counter = 0;
	ENC_ctx_slave.counter = 0;

	//Start protocol
	STSprotocol(&ENC_ctx_master, &ENC_ctx_slave, &RSA_ctx_master, &RSA_ctx_slave);
#endif
	for (bufPos = 0; bufPos < input.samplesAvailable; ) {
		placeInLargeBuffer = 0;
		while (placeInLargeBuffer < 15 * NB_OF_SMALL_BUFFERS_IN_LARGE) {
			//read a buffer
			read = wavpcm_input_read(&input, wavbuffer);
			if (read != BUFFERSIZE) {
				if ((input.samplesAvailable - bufPos) * 2 != read) {
					printf("Warning: Not a full buffer read, amount read: %d. samplesAvailable: %d. bufPos: %d",
						read, input.samplesAvailable, bufPos);
				}
			}
			//process the buffer
			bufPos += read/2;

			analysis(wavbuffer, &historyChunkAnalysis); //Split bands into subbands
#ifdef cheatMono
			if(stillMono){ //Check if new buffer is still mono
				for(i=0; i < BUFFERSIZE_DIV2; i++){
					if(wavbuffer[2*i] != wavbuffer[2*i+1]){
						stillMono = 0;
						break;
					}
				}
			}
			if(stillMono){ //if still mono, use it to cheat (de)quantize
				quantizeBandsMono((short *) (largeCryptoBuffer + placeInLargeBuffer), &historyChunkAnalysis);
			}else{
#endif
			//quantize the bands, use largeCryptoBuffer to write the output in
			quantizeBands((short *) (largeCryptoBuffer + placeInLargeBuffer), &historyChunkAnalysis);
#ifdef cheatMono
			}
#endif
			//compress this new output (in place)
			compress30Samples((short *) (largeCryptoBuffer + placeInLargeBuffer)); //Compress samples to fit into the bytes that we give to crypto

			//increment position in largeCryptoBuffer
			placeInLargeBuffer += 15;
		}
#ifdef CRYPTO
		//encrypt
		sendData(&ENC_ctx_master, largeCryptoBuffer, sizeof(largeCryptoBuffer), &ciphermessage);
		//channel

		//decrypt
		readData(&ENC_ctx_slave, &ciphermessage, largeCryptoBuffer, &decrypt_size);
#endif
		//Now dequantize and merge bands back together
		placeInLargeBuffer = 0;
		while (placeInLargeBuffer < 15 * NB_OF_SMALL_BUFFERS_IN_LARGE) {
			//decompress. This is not in place because the amount of data expands. However, wavbuffer can be used again
			decompress30samples((largeCryptoBuffer + placeInLargeBuffer), wavbuffer);

			//dequantize the bands, working in place in wavbuffer
#ifdef cheatMono
			if(stillMono){ //if still mono, use it to cheat (de)quantize
				dequantizeBandsMono(wavbuffer);
			}else{
#endif
				dequantizeBands(wavbuffer);
#ifdef cheatMono
			}
#endif
			//synthesis filter bank, again using wavbuffer for both input and output
			synthesis(wavbuffer,
				wavbuffer, wavbuffer + 10, wavbuffer + 20, wavbuffer + 5, wavbuffer + 15, wavbuffer + 25,
				&historyChunkSynthesis);
			//write the output to file
			wavpcm_output_write(&output, wavbuffer, 40);
			placeInLargeBuffer += 15;
		}
	}
	wavpcm_input_close(&input);
	wavpcm_output_close(&output);
	return 0;

}
Esempio n. 3
0
int main(int argc, char **argv) {
	size_t bufPos;
	size_t read;

	short buffer[BUFFERSIZE];
	short encoded[BUFFERSIZE];

	struct wavpcm_input input;
	struct wavpcm_output output;

	struct decode_chunk_struct decode_chunk_left;
	struct decode_chunk_struct decode_chunk_right;
	struct encode_chunk_struct encode_chunk_left;
	struct encode_chunk_struct encode_chunk_right;

    // Initializations
    srand(time(NULL));
    _convFromOctets();

    // Construct
    buffer_construct();
    channel_construct();
    sender_construct();
    receiver_construct();

    // Handshake
	#ifndef __ENC_NO_PRINTS__
		printf("\n# Key Exchange\n");
		printf("--------------\n\n");
	#endif

    handshakeState = SENDER_HELLO;
    while (HANDSHAKE_FINISHED != handshakeState)
        _handshake();

    // Transmit
	memset(&input, 0, sizeof(struct wavpcm_input));
	input.resource = INPUTWAVFILE;
	memset(&output, 0, sizeof(struct wavpcm_output));
	output.resource = OUTPUTWAVFILE;

	/* initialize structs */
    memset(&encode_chunk_left, 0, sizeof(struct encode_chunk_struct));
    memset(&encode_chunk_right, 0, sizeof(struct encode_chunk_struct));
    memset(&decode_chunk_left, 0, sizeof(struct decode_chunk_struct));
    memset(&decode_chunk_right, 0, sizeof(struct decode_chunk_struct));

    /* initializing for quantisation */
    encode_chunk_left.Qstep[0] = QSTART;
    encode_chunk_right.Qstep[0] = QSTART;
    decode_chunk_left.Qstep[0] = QSTART;
    decode_chunk_right.Qstep[0] = QSTART;
    encode_chunk_left.Qstep[1] = QSTART;
    encode_chunk_right.Qstep[1] = QSTART;
    decode_chunk_left.Qstep[1] = QSTART;
    decode_chunk_right.Qstep[1] = QSTART;
    encode_chunk_left.Qstep[2] = QSTART;
    encode_chunk_right.Qstep[2] = QSTART;
    decode_chunk_left.Qstep[2] = QSTART;
    decode_chunk_right.Qstep[2] = QSTART;
    encode_chunk_left.Qstep[3] = QSTART;
    encode_chunk_right.Qstep[3] = QSTART;
    decode_chunk_left.Qstep[3] = QSTART;
    decode_chunk_right.Qstep[3] = QSTART;

	wavpcm_input_open(&input);
	wavpcm_output_copy_settings(&input, &output);
	wavpcm_output_open(&output);

	for (bufPos = 0; bufPos < input.samplesAvailable ; bufPos += (BUFFERSIZE/2)) {
		read = wavpcm_input_read(&input, buffer);
		encode(buffer, &encode_chunk_left, &encode_chunk_right, encoded);

		while (buffer_isModified()) {}
		buffer_write((field_t *) buffer, BUFFERSIZE*sizeof(short));

		_transmit();
		receiver_receiveData();

		buffer_read((field_t *) buffer, BUFFERSIZE*sizeof(short));

		decode(&decode_chunk_left, &decode_chunk_right, encoded, buffer);
		wavpcm_output_write(&output, buffer, read);
	}

	wavpcm_output_close(&output);

    exit(EXIT_SUCCESS);
}