JavascriptProva

Visualizzazione post con etichetta audio_digitale. Mostra tutti i post
Visualizzazione post con etichetta audio_digitale. Mostra tutti i post

venerdì 18 marzo 2016

Codice sicuramente funzionante per l'AudioRecord

public class MainActivity extends Activity{
 
 Button button;
 Button button2;
 boolean isRecording;
 
 AudioRecord audioRecord;
 
 int audioSource=AudioSource.MIC;
 int sampleRate=44100;
 int channelConfig=AudioFormat.CHANNEL_IN_STEREO;
 int audioFormat=AudioFormat.ENCODING_PCM_16BIT;
 int bufferSize=AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
 
 
 //AudioTrack audioTrack=new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,channelConfig,audioFormat,bufferSize,AudioTrack.MODE_STATIC);

 short[] buffer=new short[(bufferSize/2)];
 
 
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Log.v("Buffer size",bufferSize+"");
  Log.v("Length", buffer.length+"");
  for(int i=0;i<bufferSize/2;i++){
   Log.v(i+"", buffer[i]+"");
  }
  
  
  button=(Button)findViewById(R.id.button1);
  button.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    azione();
    
   }
  });
  
  button2=(Button)findViewById(R.id.button2);
  button2.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    isRecording=false;
    
    audioRecord.stop();
    audioRecord.release();
    audioRecord=null;

    
    Log.v("spento", "SPENTO");
    //ULTIMA MODIFICA--------------------------------
    for(int i=0;i<bufferSize/2;i++){
     Log.v(i+"", (short)buffer[i]+"");
    }
   }
  });
 }
 
 public void azione(){
  audioRecord=new AudioRecord(audioSource,sampleRate,channelConfig,audioFormat,bufferSize/2);
  audioRecord.startRecording();
  isRecording=true;
  Thread thread=new Thread(new Runnable(){

   @Override
   public void run() { 
    while(isRecording){
     int rd=audioRecord.read(buffer, 0, bufferSize/2);
     
    }
     
   }
   
   
  });
  thread.start();
 }
}

Rappresentazione di un'onda sonora?

Usando AudioRecord, ho ottenuto nel buffer questi numeri:
03-18 16:09:00.370: V/0(2345): 35
03-18 16:09:00.370: V/1(2345): 35
03-18 16:09:00.370: V/2(2345): 32
03-18 16:09:00.370: V/3(2345): 32
03-18 16:09:00.370: V/4(2345): 29
03-18 16:09:00.370: V/5(2345): 29
03-18 16:09:00.370: V/6(2345): 26
03-18 16:09:00.370: V/7(2345): 26
03-18 16:09:00.370: V/8(2345): 23
03-18 16:09:00.370: V/9(2345): 23
03-18 16:09:00.370: V/10(2345): 18
03-18 16:09:00.370: V/11(2345): 18
03-18 16:09:00.370: V/12(2345): 14
03-18 16:09:00.370: V/13(2345): 14
03-18 16:09:00.370: V/14(2345): 9
03-18 16:09:00.370: V/15(2345): 9
03-18 16:09:00.370: V/16(2345): 3
03-18 16:09:00.370: V/17(2345): 3
03-18 16:09:00.370: V/18(2345): -3
03-18 16:09:00.370: V/19(2345): -3
03-18 16:09:00.370: V/20(2345): -10
03-18 16:09:00.370: V/21(2345): -10
03-18 16:09:00.370: V/22(2345): -17
03-18 16:09:00.370: V/23(2345): -17
03-18 16:09:00.370: V/24(2345): -24
03-18 16:09:00.370: V/25(2345): -24
03-18 16:09:00.370: V/26(2345): -31
03-18 16:09:00.370: V/27(2345): -31
03-18 16:09:00.370: V/28(2345): -39
03-18 16:09:00.370: V/29(2345): -39
03-18 16:09:00.370: V/30(2345): -46
03-18 16:09:00.370: V/31(2345): -46
03-18 16:09:00.370: V/32(2345): -52
03-18 16:09:00.370: V/33(2345): -52
03-18 16:09:00.370: V/34(2345): -59
03-18 16:09:00.370: V/35(2345): -59
03-18 16:09:00.370: V/36(2345): -64
03-18 16:09:00.370: V/37(2345): -64
03-18 16:09:00.370: V/38(2345): -70
03-18 16:09:00.370: V/39(2345): -70
03-18 16:09:00.370: V/40(2345): -74
03-18 16:09:00.370: V/41(2345): -74
03-18 16:09:00.370: V/42(2345): -79
03-18 16:09:00.370: V/43(2345): -79
03-18 16:09:00.370: V/44(2345): -82
03-18 16:09:00.370: V/45(2345): -82
03-18 16:09:00.370: V/46(2345): -86
03-18 16:09:00.370: V/47(2345): -86
03-18 16:09:00.370: V/48(2345): -90
03-18 16:09:00.370: V/49(2345): -90
03-18 16:09:00.370: V/50(2345): -94
03-18 16:09:00.370: V/51(2345): -94
03-18 16:09:00.370: V/52(2345): -97
03-18 16:09:00.370: V/53(2345): -97
03-18 16:09:00.370: V/54(2345): -101
03-18 16:09:00.370: V/55(2345): -101
03-18 16:09:00.370: V/56(2345): -106
03-18 16:09:00.370: V/57(2345): -106
03-18 16:09:00.370: V/58(2345): -110
03-18 16:09:00.370: V/59(2345): -110
03-18 16:09:00.370: V/60(2345): -115
03-18 16:09:00.370: V/61(2345): -115
03-18 16:09:00.370: V/62(2345): -119
03-18 16:09:00.370: V/63(2345): -119
03-18 16:09:00.370: V/64(2345): -124
03-18 16:09:00.370: V/65(2345): -124
03-18 16:09:00.370: V/66(2345): -128
03-18 16:09:00.370: V/67(2345): -128
03-18 16:09:00.370: V/68(2345): -131
03-18 16:09:00.370: V/69(2345): -131
03-18 16:09:00.370: V/70(2345): -134
03-18 16:09:00.370: V/71(2345): -134
03-18 16:09:00.370: V/72(2345): -137
03-18 16:09:00.370: V/73(2345): -137
03-18 16:09:00.370: V/74(2345): -138
03-18 16:09:00.370: V/75(2345): -138
03-18 16:09:00.370: V/76(2345): -139
03-18 16:09:00.370: V/77(2345): -139
03-18 16:09:00.370: V/78(2345): -139
03-18 16:09:00.370: V/79(2345): -139
03-18 16:09:00.370: V/80(2345): -138
03-18 16:09:00.370: V/81(2345): -138
03-18 16:09:00.370: V/82(2345): -136
03-18 16:09:00.370: V/83(2345): -136
03-18 16:09:00.370: V/84(2345): -134
03-18 16:09:00.370: V/85(2345): -134
03-18 16:09:00.370: V/86(2345): -130
03-18 16:09:00.370: V/87(2345): -130
03-18 16:09:00.370: V/88(2345): -126
03-18 16:09:00.370: V/89(2345): -126
03-18 16:09:00.370: V/90(2345): -120
03-18 16:09:00.370: V/91(2345): -120
03-18 16:09:00.370: V/92(2345): -114
03-18 16:09:00.370: V/93(2345): -114
03-18 16:09:00.370: V/94(2345): -108
03-18 16:09:00.370: V/95(2345): -108
03-18 16:09:00.370: V/96(2345): -100
03-18 16:09:00.370: V/97(2345): -100
03-18 16:09:00.370: V/98(2345): -93
03-18 16:09:00.370: V/99(2345): -93
03-18 16:09:00.370: V/100(2345): -85
03-18 16:09:00.370: V/101(2345): -85
03-18 16:09:00.370: V/102(2345): -77
03-18 16:09:00.370: V/103(2345): -77
03-18 16:09:00.370: V/104(2345): -70
03-18 16:09:00.370: V/105(2345): -70
03-18 16:09:00.370: V/106(2345): -62
03-18 16:09:00.370: V/107(2345): -62
03-18 16:09:00.370: V/108(2345): -55
03-18 16:09:00.370: V/109(2345): -55
03-18 16:09:00.370: V/110(2345): -48
03-18 16:09:00.370: V/111(2345): -48
03-18 16:09:00.370: V/112(2345): -41
03-18 16:09:00.370: V/113(2345): -41
03-18 16:09:00.370: V/114(2345): -35
03-18 16:09:00.370: V/115(2345): -35
03-18 16:09:00.370: V/116(2345): -29
03-18 16:09:00.370: V/117(2345): -29
03-18 16:09:00.370: V/118(2345): -23
03-18 16:09:00.370: V/119(2345): -23
03-18 16:09:00.370: V/120(2345): -17
03-18 16:09:00.370: V/121(2345): -17
03-18 16:09:00.370: V/122(2345): -11
03-18 16:09:00.370: V/123(2345): -11
03-18 16:09:00.370: V/124(2345): -5
03-18 16:09:00.370: V/125(2345): -5
03-18 16:09:00.370: V/126(2345): 1
03-18 16:09:00.370: V/127(2345): 1
03-18 16:09:00.370: V/128(2345): 8
03-18 16:09:00.370: V/129(2345): 8
03-18 16:09:00.370: V/130(2345): 15
03-18 16:09:00.370: V/131(2345): 15
03-18 16:09:00.370: V/132(2345): 22
03-18 16:09:00.370: V/133(2345): 22
03-18 16:09:00.370: V/134(2345): 29
03-18 16:09:00.370: V/135(2345): 29
03-18 16:09:00.370: V/136(2345): 37
03-18 16:09:00.370: V/137(2345): 37
03-18 16:09:00.370: V/138(2345): 45
03-18 16:09:00.370: V/139(2345): 45
03-18 16:09:00.370: V/140(2345): 53
03-18 16:09:00.370: V/141(2345): 53
03-18 16:09:00.370: V/142(2345): 60
03-18 16:09:00.370: V/143(2345): 60
03-18 16:09:00.370: V/144(2345): 68
03-18 16:09:00.370: V/145(2345): 68
03-18 16:09:00.370: V/146(2345): 74
03-18 16:09:00.370: V/147(2345): 74
03-18 16:09:00.370: V/148(2345): 81
03-18 16:09:00.370: V/149(2345): 81
03-18 16:09:00.370: V/150(2345): 87
03-18 16:09:00.370: V/151(2345): 87
03-18 16:09:00.370: V/152(2345): 92
03-18 16:09:00.370: V/153(2345): 92
03-18 16:09:00.370: V/154(2345): 97
03-18 16:09:00.370: V/155(2345): 97
03-18 16:09:00.370: V/156(2345): 101
03-18 16:09:00.370: V/157(2345): 101
03-18 16:09:00.370: V/158(2345): 105
03-18 16:09:00.370: V/159(2345): 105
03-18 16:09:00.370: V/160(2345): 108
03-18 16:09:00.370: V/161(2345): 108
03-18 16:09:00.370: V/162(2345): 110
03-18 16:09:00.370: V/163(2345): 110
03-18 16:09:00.370: V/164(2345): 112
03-18 16:09:00.370: V/165(2345): 112
03-18 16:09:00.370: V/166(2345): 114
03-18 16:09:00.370: V/167(2345): 114
03-18 16:09:00.370: V/168(2345): 116
03-18 16:09:00.370: V/169(2345): 116
03-18 16:09:00.370: V/170(2345): 117
03-18 16:09:00.370: V/171(2345): 117
03-18 16:09:00.370: V/172(2345): 118
03-18 16:09:00.370: V/173(2345): 118
03-18 16:09:00.370: V/174(2345): 119
03-18 16:09:00.370: V/175(2345): 119
03-18 16:09:00.370: V/176(2345): 120
03-18 16:09:00.370: V/177(2345): 120
03-18 16:09:00.370: V/178(2345): 121
03-18 16:09:00.370: V/179(2345): 121
03-18 16:09:00.370: V/180(2345): 122
03-18 16:09:00.370: V/181(2345): 122
03-18 16:09:00.370: V/182(2345): 123
03-18 16:09:00.370: V/183(2345): 123
03-18 16:09:00.370: V/184(2345): 125
03-18 16:09:00.370: V/185(2345): 125
03-18 16:09:00.370: V/186(2345): 127
03-18 16:09:00.370: V/187(2345): 127
03-18 16:09:00.370: V/188(2345): 129
03-18 16:09:00.370: V/189(2345): 129
03-18 16:09:00.370: V/190(2345): 131
03-18 16:09:00.370: V/191(2345): 131
03-18 16:09:00.370: V/192(2345): 133
03-18 16:09:00.370: V/193(2345): 133
03-18 16:09:00.370: V/194(2345): 135
03-18 16:09:00.370: V/195(2345): 135
03-18 16:09:00.370: V/196(2345): 136
03-18 16:09:00.370: V/197(2345): 136
03-18 16:09:00.370: V/198(2345): 137
03-18 16:09:00.370: V/199(2345): 137
03-18 16:09:00.370: V/200(2345): 138
03-18 16:09:00.370: V/201(2345): 138
03-18 16:09:00.370: V/202(2345): 137
03-18 16:09:00.370: V/203(2345): 137
03-18 16:09:00.370: V/204(2345): 136
03-18 16:09:00.370: V/205(2345): 136
03-18 16:09:00.370: V/206(2345): 133
03-18 16:09:00.370: V/207(2345): 133
03-18 16:09:00.370: V/208(2345): 129
03-18 16:09:00.370: V/209(2345): 129
03-18 16:09:00.370: V/210(2345): 125
03-18 16:09:00.370: V/211(2345): 125
03-18 16:09:00.370: V/212(2345): 119
03-18 16:09:00.370: V/213(2345): 119
03-18 16:09:00.370: V/214(2345): 113
03-18 16:09:00.370: V/215(2345): 113
03-18 16:09:00.370: V/216(2345): 106
03-18 16:09:00.370: V/217(2345): 106
03-18 16:09:00.370: V/218(2345): 98
03-18 16:09:00.370: V/219(2345): 98
03-18 16:09:00.370: V/220(2345): 90
03-18 16:09:00.370: V/221(2345): 90
03-18 16:09:00.370: V/222(2345): 82
03-18 16:09:00.370: V/223(2345): 82
03-18 16:09:00.370: V/224(2345): 74
03-18 16:09:00.370: V/225(2345): 74
03-18 16:09:00.370: V/226(2345): 65
03-18 16:09:00.370: V/227(2345): 65
03-18 16:09:00.370: V/228(2345): 56
03-18 16:09:00.370: V/229(2345): 56
03-18 16:09:00.370: V/230(2345): 48
03-18 16:09:00.370: V/231(2345): 48
03-18 16:09:00.370: V/232(2345): 39
03-18 16:09:00.370: V/233(2345): 39
03-18 16:09:00.370: V/234(2345): 30
03-18 16:09:00.370: V/235(2345): 30
03-18 16:09:00.370: V/236(2345): 21
03-18 16:09:00.370: V/237(2345): 21
03-18 16:09:00.370: V/238(2345): 12
03-18 16:09:00.370: V/239(2345): 12
03-18 16:09:00.370: V/240(2345): 2
03-18 16:09:00.370: V/241(2345): 2
03-18 16:09:00.370: V/242(2345): -7
03-18 16:09:00.370: V/243(2345): -7
03-18 16:09:00.370: V/244(2345): -16
03-18 16:09:00.370: V/245(2345): -16
03-18 16:09:00.370: V/246(2345): -25
03-18 16:09:00.370: V/247(2345): -25
03-18 16:09:00.370: V/248(2345): -35
03-18 16:09:00.370: V/249(2345): -35
03-18 16:09:00.370: V/250(2345): -44
03-18 16:09:00.370: V/251(2345): -44
03-18 16:09:00.370: V/252(2345): -54
03-18 16:09:00.370: V/253(2345): -54
03-18 16:09:00.370: V/254(2345): -64
03-18 16:09:00.370: V/255(2345): -64
03-18 16:09:00.370: V/256(2345): -74
03-18 16:09:00.370: V/257(2345): -74
03-18 16:09:00.370: V/258(2345): -84
03-18 16:09:00.370: V/259(2345): -84
03-18 16:09:00.370: V/260(2345): -94
03-18 16:09:00.370: V/261(2345): -94
03-18 16:09:00.370: V/262(2345): -104
03-18 16:09:00.370: V/263(2345): -104
03-18 16:09:00.370: V/264(2345): -114
03-18 16:09:00.370: V/265(2345): -114
03-18 16:09:00.370: V/266(2345): -123
03-18 16:09:00.370: V/267(2345): -123
03-18 16:09:00.370: V/268(2345): -132
03-18 16:09:00.370: V/269(2345): -132
03-18 16:09:00.370: V/270(2345): -141
03-18 16:09:00.370: V/271(2345): -141
03-18 16:09:00.370: V/272(2345): -149
03-18 16:09:00.370: V/273(2345): -149
03-18 16:09:00.370: V/274(2345): -156
03-18 16:09:00.370: V/275(2345): -156
03-18 16:09:00.370: V/276(2345): -162
03-18 16:09:00.370: V/277(2345): -162
Un andamento sinusoidale? Sto VEDENDO LA RAPPRESENTAZIONE DI UN'ONDA SONORA????? Non ne sono sicuro, ma sarebbe pazzescamente FANTASTICO!!!

lunedì 14 marzo 2016

audioRecorder, primi rudimenti.

Mi preparo tutte le impostazioni relative al tipo di segnale digitale, e dichiaro la variabile per l'audioRecorder e per il Thread (è ovvio che la registrazione va su un thread separato altrimenti si impappa tutto),
Poi ho due metodi: startRecording, che istanzia un audioRecorder e lo avvia sul thread suo leggendo i dati che arrivano dal microfono (o altra fonte) nel buffer; e stopRecording che stoppa il loop della registrazione, ferma l'audioRecorder e me lo distrugge.

Ecco le dichiarazioni:
 private Thread recordingThread=null;
 boolean isRecording;
 Button button1;
 
 int audioSource=AudioSource.MIC;
 int sampleRate=8000;
 int channelConfig=AudioFormat.CHANNEL_IN_MONO;
 int audioFormat=AudioFormat.ENCODING_PCM_16BIT;
 int bufferSizeInBytes=AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
 AudioRecord audioRecord;
 byte[] buffer=new byte[bufferSizeInBytes];
E il codice che attribuisce il listener al bottone che avvia e stoppa la registrazione:
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  button1=(Button)findViewById(R.id.button1);
  button1.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    if(isRecording){
     ((Button)v).setText("Stop recording");
     stopRecording();
    }
    else{
     ((Button)v).setText("Start recording");
     startRecording();
    }
    
    
   }
  });


Poi il codice che crea e quello che distrugge l'audioRecorder:
La prima funzione è startRecording:
 public void startRecording(){
  isRecording=true;
  audioRecord=new AudioRecord(audioSource,sampleRate,channelConfig,audioFormat,bufferSizeInBytes);
  recordingThread=new Thread(new Runnable(){

   @Override
   public void run() {
    while(isRecording){
     audioRecord.read(buffer,0,bufferSizeInBytes);
    }
    
   }
   
  });
  
 }


E l'altra è stopRecording:
 public void stopRecording(){
  isRecording=false;
  audioRecord.stop();
  audioRecord.release();
  audioRecord=null; 
 }
E pare che funzioni.
Ovviamente non lo posso testare perché mi manca ancora uno strumento con funzioni di player...
Comunque perlomeno non si blocca, ed è già qualcosa.