JavascriptProva

lunedì 21 marzo 2016

FileInputStream

Ho messo un file wav nella memoria dell'emulatore: si tratta di un file che ho preso da qualche parte, contenente una canzoncina comica, tanto per mantenere l'allegria, e voglio leggere il file in un buffer.
Ecco il codice:
public class MainActivity extends Activity {

// dichiarazioni di variabili oggetto dell'activity
 Button button;
 Button button2;
 Button button3;
 Button button4;

//il percorso del file
 String filePath = Environment.getExternalStorageDirectory()
   .getAbsolutePath() + "/vaffanculo.wav";

// registratore e riproduttore (dichiarazioni)
 AudioRecord audioRecord;
 AudioTrack audioTrack;

//impostazioni audio
 int audioSource=AudioSource.MIC;
 int sampleRate=44100;
 int channelConfig=AudioFormat.CHANNEL_IN_STEREO;
 int audioFormat=AudioFormat.ENCODING_PCM_16BIT;
 int minBufferSize=AudioRecord.getMinBufferSize(sampleRate, 
             channelConfig, 
             audioFormat);
 
// dichiarazione del buffer
 byte[] buffer= new byte[minBufferSize];
 
 
 
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  
//istanziazione dei bottoni
  button = (Button) findViewById(R.id.button1);
  button2 = (Button) findViewById(R.id.button2);
  button3 = (Button) findViewById(R.id.button3);
  button4 = (Button) findViewById(R.id.button4);

//listeners
  button.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    try {
     
     FileInputStream fis=new FileInputStream(filePath);
     fis.read(buffer,0,minBufferSize);
     for(int i=0; i < minBufferSize;i++){
      Log.v(i+"",buffer[i]+"");
     }
    } catch (FileNotFoundException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }
    
    
   }
  });
    

  button2.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
    
   }
  });
  button3.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    
   }
  });
  
  button4.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    
   }
  });
  
// termine listeners
 }

}
La parte notevole è questa:
  button.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    try {
     
     FileInputStream fis=new FileInputStream(filePath);
     fis.read(buffer,0,minBufferSize);
     for(int i=0; i < minBufferSize;i++){
      Log.v(i+"",buffer[i]+"");
     }
    } catch (FileNotFoundException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }
    
    
   }
  });
Creo un FileInputStream con l'indirizzo del file, e quindi con la riga scritta in verde "leggo" i bytes del file nel buffer.
Quindi schiaccio il bottone, produco il LogCat e mi leggo pezzo per pezzo ogni byte, di cui riporto i primi 100:
03-21 08:22:49.520: V/0(19511): 74
03-21 08:22:49.520: V/1(19511): 1
03-21 08:22:49.520: V/2(19511): 74
03-21 08:22:49.520: V/3(19511): 1
03-21 08:22:49.520: V/4(19511): 50
03-21 08:22:49.520: V/5(19511): 1
03-21 08:22:49.520: V/6(19511): 50
03-21 08:22:49.520: V/7(19511): 1
03-21 08:22:49.520: V/8(19511): 22
03-21 08:22:49.520: V/9(19511): 1
03-21 08:22:49.520: V/10(19511): 22
03-21 08:22:49.520: V/11(19511): 1
03-21 08:22:49.520: V/12(19511): -9
03-21 08:22:49.520: V/13(19511): 0
03-21 08:22:49.520: V/14(19511): -9
03-21 08:22:49.520: V/15(19511): 0
03-21 08:22:49.520: V/16(19511): -42
03-21 08:22:49.520: V/17(19511): 0
03-21 08:22:49.520: V/18(19511): -42
03-21 08:22:49.520: V/19(19511): 0
03-21 08:22:49.520: V/20(19511): -77
03-21 08:22:49.520: V/21(19511): 0
03-21 08:22:49.520: V/22(19511): -77
03-21 08:22:49.520: V/23(19511): 0
03-21 08:22:49.520: V/24(19511): -112
03-21 08:22:49.520: V/25(19511): 0
03-21 08:22:49.520: V/26(19511): -112
03-21 08:22:49.520: V/27(19511): 0
03-21 08:22:49.520: V/28(19511): 110
03-21 08:22:49.520: V/29(19511): 0
03-21 08:22:49.520: V/30(19511): 110
03-21 08:22:49.520: V/31(19511): 0
03-21 08:22:49.520: V/32(19511): 79
03-21 08:22:49.520: V/33(19511): 0
03-21 08:22:49.520: V/34(19511): 79
03-21 08:22:49.520: V/35(19511): 0
03-21 08:22:49.520: V/36(19511): 51
03-21 08:22:49.520: V/37(19511): 0
03-21 08:22:49.520: V/38(19511): 51
03-21 08:22:49.520: V/39(19511): 0
03-21 08:22:49.520: V/40(19511): 27
03-21 08:22:49.520: V/41(19511): 0
03-21 08:22:49.520: V/42(19511): 27
03-21 08:22:49.520: V/43(19511): 0
03-21 08:22:49.520: V/44(19511): 8
03-21 08:22:49.520: V/45(19511): 0
03-21 08:22:49.520: V/46(19511): 8
03-21 08:22:49.520: V/47(19511): 0
03-21 08:22:49.520: V/48(19511): -8
03-21 08:22:49.520: V/49(19511): -1
03-21 08:22:49.520: V/50(19511): -8
03-21 08:22:49.520: V/51(19511): -1
03-21 08:22:49.520: V/52(19511): -22
03-21 08:22:49.520: V/53(19511): -1
03-21 08:22:49.520: V/54(19511): -22
03-21 08:22:49.520: V/55(19511): -1
03-21 08:22:49.520: V/56(19511): -35
03-21 08:22:49.520: V/57(19511): -1
03-21 08:22:49.520: V/58(19511): -35
03-21 08:22:49.520: V/59(19511): -1
03-21 08:22:49.520: V/60(19511): -48
03-21 08:22:49.520: V/61(19511): -1
03-21 08:22:49.520: V/62(19511): -48
03-21 08:22:49.520: V/63(19511): -1
03-21 08:22:49.520: V/64(19511): -62
03-21 08:22:49.520: V/65(19511): -1
03-21 08:22:49.520: V/66(19511): -62
03-21 08:22:49.520: V/67(19511): -1
03-21 08:22:49.520: V/68(19511): -78
03-21 08:22:49.520: V/69(19511): -1
03-21 08:22:49.520: V/70(19511): -78
03-21 08:22:49.520: V/71(19511): -1
03-21 08:22:49.520: V/72(19511): -96
03-21 08:22:49.520: V/73(19511): -1
03-21 08:22:49.520: V/74(19511): -96
03-21 08:22:49.520: V/75(19511): -1
03-21 08:22:49.520: V/76(19511): -114
03-21 08:22:49.520: V/77(19511): -1
03-21 08:22:49.520: V/78(19511): -114
03-21 08:22:49.520: V/79(19511): -1
03-21 08:22:49.520: V/80(19511): 125
03-21 08:22:49.520: V/81(19511): -1
03-21 08:22:49.520: V/82(19511): 125
03-21 08:22:49.520: V/83(19511): -1
03-21 08:22:49.520: V/84(19511): 109
03-21 08:22:49.520: V/85(19511): -1
03-21 08:22:49.520: V/86(19511): 109
03-21 08:22:49.520: V/87(19511): -1
03-21 08:22:49.520: V/88(19511): 96
03-21 08:22:49.520: V/89(19511): -1
03-21 08:22:49.520: V/90(19511): 96
03-21 08:22:49.520: V/91(19511): -1
03-21 08:22:49.520: V/92(19511): 87
03-21 08:22:49.520: V/93(19511): -1
03-21 08:22:49.520: V/94(19511): 87
03-21 08:22:49.520: V/95(19511): -1
03-21 08:22:49.520: V/96(19511): 82
03-21 08:22:49.520: V/97(19511): -1
03-21 08:22:49.520: V/98(19511): 82
03-21 08:22:49.520: V/99(19511): -1
03-21 08:22:49.520: V/100(19511): 81
Sono 278 bytes in totale. Ma il buffer di che dimensione è?
Modifico il codice per ottenere minBufferSize in LogCat:

     FileInputStream fis=new FileInputStream(filePath);
     fis.read(buffer,0,minBufferSize);
     for(int i=0; i < minBufferSize;i++){
      Log.v("BUFFERSIZE",i+"",buffer[i]+"");
     }
.....   
Ottengo 640, che è pure il valore che ottengo come risultato della funzione fis.read(buffer,0,minBufferSize).

Ma adesso voglio trovare modi diversi di inserire i valori nel buffer.
     FileInputStream fis=new FileInputStream(filePath);
     for(int i=0; i < minBufferSize;i++){
      buffer[i]=(byte)fis.read();
     }
     for(int i=0; i < minBufferSize;i++){
      Log.v(i+"",buffer[i]+"");
     }
ed ecco il risultato:
03-21 08:59:47.970: V/0(26357): 74
03-21 08:59:47.970: V/1(26357): 1
03-21 08:59:47.970: V/2(26357): 74
03-21 08:59:47.970: V/3(26357): 1
03-21 08:59:47.970: V/4(26357): 50
03-21 08:59:47.970: V/5(26357): 1
03-21 08:59:47.970: V/6(26357): 50
03-21 08:59:47.970: V/7(26357): 1
03-21 08:59:47.970: V/8(26357): 22
03-21 08:59:47.970: V/9(26357): 1
03-21 08:59:47.970: V/10(26357): 22
03-21 08:59:47.970: V/11(26357): 1
03-21 08:59:47.970: V/12(26357): -9
03-21 08:59:47.970: V/13(26357): 0
03-21 08:59:47.970: V/14(26357): -9
03-21 08:59:47.970: V/15(26357): 0
03-21 08:59:47.970: V/16(26357): -42
03-21 08:59:47.970: V/17(26357): 0
03-21 08:59:47.970: V/18(26357): -42
03-21 08:59:47.970: V/19(26357): 0
03-21 08:59:47.970: V/20(26357): -77
03-21 08:59:47.970: V/21(26357): 0
03-21 08:59:47.970: V/22(26357): -77
03-21 08:59:47.970: V/23(26357): 0
03-21 08:59:47.970: V/24(26357): -112
03-21 08:59:47.970: V/25(26357): 0
03-21 08:59:47.970: V/26(26357): -112
03-21 08:59:47.970: V/27(26357): 0
03-21 08:59:47.970: V/28(26357): 110
03-21 08:59:47.970: V/29(26357): 0
03-21 08:59:47.970: V/30(26357): 110
03-21 08:59:47.970: V/31(26357): 0
03-21 08:59:47.970: V/32(26357): 79
03-21 08:59:47.970: V/33(26357): 0
03-21 08:59:47.970: V/34(26357): 79
03-21 08:59:47.970: V/35(26357): 0
03-21 08:59:47.970: V/36(26357): 51
03-21 08:59:47.970: V/37(26357): 0
03-21 08:59:47.970: V/38(26357): 51
03-21 08:59:47.970: V/39(26357): 0
03-21 08:59:47.970: V/40(26357): 27
03-21 08:59:47.970: V/41(26357): 0
03-21 08:59:47.970: V/42(26357): 27
03-21 08:59:47.970: V/43(26357): 0
03-21 08:59:47.970: V/44(26357): 8
03-21 08:59:47.970: V/45(26357): 0
03-21 08:59:47.970: V/46(26357): 8
03-21 08:59:47.970: V/47(26357): 0
03-21 08:59:47.970: V/48(26357): -8
03-21 08:59:47.970: V/49(26357): -1
03-21 08:59:47.970: V/50(26357): -8
03-21 08:59:47.970: V/51(26357): -1
03-21 08:59:47.970: V/52(26357): -22
03-21 08:59:47.970: V/53(26357): -1
03-21 08:59:47.970: V/54(26357): -22
03-21 08:59:47.970: V/55(26357): -1
03-21 08:59:47.970: V/56(26357): -35
03-21 08:59:47.970: V/57(26357): -1
03-21 08:59:47.970: V/58(26357): -35
03-21 08:59:47.970: V/59(26357): -1
03-21 08:59:47.970: V/60(26357): -48
03-21 08:59:47.970: V/61(26357): -1
03-21 08:59:47.970: V/62(26357): -48
03-21 08:59:47.970: V/63(26357): -1
03-21 08:59:47.970: V/64(26357): -62
03-21 08:59:47.970: V/65(26357): -1
03-21 08:59:47.970: V/66(26357): -62
03-21 08:59:47.970: V/67(26357): -1
03-21 08:59:47.970: V/68(26357): -78
03-21 08:59:47.970: V/69(26357): -1
03-21 08:59:47.970: V/70(26357): -78
03-21 08:59:47.970: V/71(26357): -1
03-21 08:59:47.970: V/72(26357): -96
03-21 08:59:47.970: V/73(26357): -1
03-21 08:59:47.970: V/74(26357): -96
03-21 08:59:47.970: V/75(26357): -1
03-21 08:59:47.970: V/76(26357): -114
03-21 08:59:47.970: V/77(26357): -1
03-21 08:59:47.970: V/78(26357): -114
03-21 08:59:47.970: V/79(26357): -1
03-21 08:59:47.970: V/80(26357): 125
03-21 08:59:47.970: V/81(26357): -1
03-21 08:59:47.970: V/82(26357): 125
03-21 08:59:47.970: V/83(26357): -1
03-21 08:59:47.970: V/84(26357): 109
03-21 08:59:47.970: V/85(26357): -1
03-21 08:59:47.970: V/86(26357): 109
03-21 08:59:47.970: V/87(26357): -1
03-21 08:59:47.970: V/88(26357): 96
03-21 08:59:47.970: V/89(26357): -1
03-21 08:59:47.970: V/90(26357): 96
03-21 08:59:47.970: V/91(26357): -1
03-21 08:59:47.970: V/92(26357): 87
03-21 08:59:47.970: V/93(26357): -1
03-21 08:59:47.970: V/94(26357): 87
03-21 08:59:47.970: V/95(26357): -1
03-21 08:59:47.970: V/96(26357): 82
03-21 08:59:47.970: V/97(26357): -1
03-21 08:59:47.970: V/98(26357): 82
03-21 08:59:47.970: V/99(26357): -1
03-21 08:59:47.970: V/100(26357): 81
Questi valori sono esattamente uguali a quelli ricavati prima.

Nessun commento:

Posta un commento