Ejemplo n.º 1
0
void TMRpcm::play(SDFile myFile) {
    sdFile = myFile;
    if(speakerPin != lastSpeakPin) {
        setPin();
        lastSpeakPin=speakerPin;
    }
    stopPlayback();

    if(!wavInfo(myFile) ) {
        return;    //verify its a valid wav file
    }
    if(myFile.isOpen()) {

        playing = 1;
        myFile.seekSet(44); //skip the header info
        for(int i=0; i<buffSize; i++) {
            buffer[0][i] = myFile.read();
        }
        whichBuff = false;
        buffEmpty[0] = false;
        buffEmpty[1] = true;


        //if(pwmMode){resolution = 8;}else{resolution = 4;}
        resolution = 8;
        resolution = resolution * (1000000/SAMPLE_RATE); //Serial.println(resolution);
        volModMax = (resolution*1.5) / 248 ;
        volMod = constrain(volMod, 1, volModMax);

        //noInterrupts();
        *ICRn[tt] = resolution;
        *OCRnA[tt] = *OCRnB[tt] = 1;
        //if(pwmMode){

        *TCCRnA[tt] = _BV(WGM11) | _BV(COM1A1) | _BV(COM1B0) | _BV(COM1B1); //WGM11,12,13 all set to 1 = fast PWM/w ICR TOP
        *TCCRnB[tt] = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
        //  }else{
        //   Serial.println("A punto de comenzar a reproducir");
        //   *TCCRnA[tt] = _BV(COM1A1) | _BV(COM1B0) | _BV(COM1B1);
        //   *TCCRnB[tt] = _BV(WGM13) | _BV(CS10);
        // }
        *TIMSK[tt] = ( _BV(ICIE1) | _BV(TOIE1) );
        //interrupts();

    } else {
        Serial.println("Read fail");
    }
}
Ejemplo n.º 2
0
void TMRpcm::play(char* filename){

  pinMode(speakerPin, OUTPUT);
  stopPlayback();
  Serial.print("Playing: ");Serial.println(filename);

  if(!wavInfo(filename) ){ return; }//verify its a valid wav file
  if(sFile){sFile.close();}
  sFile = SD.open(filename);

  if(sFile){

//    wavInfo(filename); //verify its a valid wav file

    sFile.seek(44); //skip the header info
    for(int i=0; i<buffSize; i++){ buffer[0][i] = sFile.read(); }
    whichBuff = false; buffEmpty[0] = false; buffEmpty[1] = true;

    startPlayback();

    //Serial.println(cycles);
  }else{Serial.println("failed to open music file"); }

}
Ejemplo n.º 3
0
void TMRpcm::play(char* filename, unsigned long seekPoint){

  if(speakerPin != lastSpeakPin){
	  #if !defined (MODE2)
	  	setPin();
	  #else
	    setPins();
	  #endif
	  lastSpeakPin=speakerPin;
   }
  stopPlayback();
  if(!wavInfo(filename)){
  	#if defined (debug)
  		Serial.println("WAV ERROR");
  	#endif
  return;
  }//verify its a valid wav file


  		if(seekPoint > 0){ seekPoint = (SAMPLE_RATE*seekPoint) + fPosition();
  		seek(seekPoint); //skip the header info

  }
	playing = 1; bitClear(optionByte,7); //paused = 0;

	if(SAMPLE_RATE > 45050 ){ SAMPLE_RATE = 24000;
	#if defined (debug)
  	  	Serial.print("SAMPLE RATE TOO HIGH: ");
  	  	Serial.println(SAMPLE_RATE);
  	#endif
  	}

#if !defined (USE_TIMER2)
    //if(qual)
    if(bitRead(optionByte,6)){resolution = 10 * (800000/SAMPLE_RATE);}
    else{ resolution = 10 * (1600000/SAMPLE_RATE);
	}
#else
	resolution = 255;
	if(SAMPLE_RATE < 9000){
		*TCCRnB[tt] &= ~_BV(CS20);
		*TCCRnB[tt] |= _BV(CS21);
	}else{
		*TCCRnB[tt] &= ~_BV(CS21);
		*TCCRnB[tt] |= _BV(CS20);
	}
#endif
    byte tmp = (sFile.read() + sFile.peek()) / 2;

	#if defined(rampMega)
    if(bitRead(optionByte,5)){

			*OCRnA[tt] = 0; *OCRnB[tt] = resolution;
			timerSt();
			for(unsigned int i=0; i < resolution; i++){

				*OCRnB[tt] = constrain(resolution-i,0,resolution);

			//if(bitRead(*TCCRnB[tt],0)){
			//	for(int i=0; i<10; i++){
			//		while(*TCNT[tt] < resolution-50){}
			//	}
			//}else{
				delayMicroseconds(150);
			//}
			}

	}
	bitClear(optionByte,5);
	#endif

	//rampUp = 0;
	unsigned int mod;
	if(volMod > 0){ mod = *OCRnA[tt] >> volMod; }else{ mod = *OCRnA[tt] << (volMod*-1); }