Wave File Not Correctly Created

This is a discussion on Wave File Not Correctly Created within the Android Audio forums, part of the Android Discussions category; hi everyone, im trying to generate a tone from a tablet' speaker at a certain frequency and then save it to a wave file without ...

Results 1 to 2 of 2

Thread: Wave File Not Correctly Created

  1. #1
    Android Lurker hanaa's Avatar
    Join Date
    Apr 2014
    Posts
    1

    Wave File Not Correctly Created

    hi everyone, im trying to generate a tone from a tablet' speaker at a certain frequency and then save it to a wave file without using the microphone (for some testing purposes).
    i followed some codes on how to build the wave file format manually. the pbroblem is when i try manipulating the wave file on Octave it gives me this error:" waveread: file contains no format chunk".
    so that means there s something wrong with the format i built. Can you please help me?
    here's the code i wrote:

    Code:
    public class MainActivity extends Activity {      
        int duration=1;
        int sampleRate=44100;
        int numSample=duration*sampleRate;
        double sample[]=new double[numSample];
        double freq1=22000;
        double freq2=23000;
        byte[] generatedSnd= new byte[2*numSample];
        Handler handler=new Handler();
        long mySubChunk1Size = 16;
        short myBitsPerSample= 16;
        int myFormat = 1;
        int myChannels = 1;
        long myByteRate = sampleRate * myChannels * myBitsPerSample/8;
        int myBlockAlign = (int) (myChannels * myBitsPerSample/8);
        long myChunk2Size = generatedSnd.length* myChannels * myBitsPerSample/8;
        long myChunkSize = 36 + myChunk2Size;
    
    
        
        @Override 
     protected void onCreate(Bundle savedInstanceState) {  
    
    
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);
    Thread thread=new Thread(new Runnable(){
        public void run(){
            try {
                genTone();
            } catch (IOException e) {
                // TODO Auto-generated catch block                                                                                                                                                                                          
                                                                                                                                                                                                                  
                e.printStackTrace();
            }
            handler.post(new Runnable(){
                public void run(){
                    playSound();
                }
            });
        }
    });
    thread.start();
    
    
       }    
        
    protected void onResume()
    {
        super.onResume();
        
    }
        void genTone() throws IOException{
            
            double instfreq=0, numerator;
    
    
            for (int i=0;i<numSample; i++ )
            {
                numerator=(double)(i)/(double)numSample;
                 instfreq    =freq1+(numerator*(freq2-freq1));
                 if ((i % 1000) == 0) {
                        Log.e("Current Freq:", String.format("Freq is:  %f at loop %d of %d", instfreq, i, numSample));
                    }
                sample[i]=Math.sin(2*Math.PI*i/(sampleRate/instfreq));
                
            }
             int idx = 0;
                for (final double dVal : sample) {
                    // scale to maximum amplitude
                    final short val = (short) ((dVal * 32767)); // max positive sample for signed 16 bit integers is 32767
                    // in 16 bit wave PCM, first byte is the low order byte (pcm: pulse control modulation)
                    generatedSnd[idx++] = (byte) (val & 0x00ff);
                    generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
    
    
    
    
                }
              DataOutputStream dd=new DataOutputStream( new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.wav" ));       
              dd.writeBytes("RIFF");
              dd.writeInt(0); // Final file size not known yet, write 0 
              dd.writeBytes("WAVE");
              dd.writeBytes("fmt ");
              dd.writeInt(Integer.reverseBytes(16)); // Sub-chunk size, 16 for PCM
              dd.writeShort(Short.reverseBytes((short) 1)); // AudioFormat, 1 for PCM
              dd.writeShort(Short.reverseBytes( (short) myChannels));// Number of channels, 1 for mono, 2 for stereo
              dd.writeInt(Integer.reverseBytes(sampleRate)); // Sample rate
              dd.writeInt(Integer.reverseBytes( (sampleRate*myBitsPerSample*myChannels/8))); // Byte rate, SampleRate*NumberOfChannels*BitsPerSample/8
              dd.writeShort(Short.reverseBytes((short) (myChannels*myBitsPerSample/8))); // Block align, NumberOfChannels*BitsPerSample/8
              dd.writeShort(Short.reverseBytes( myBitsPerSample)); // Bits per sample
              dd.writeBytes("data");
              dd.writeInt(0); // Data chunk size not known yet, write 0
              dd.write(generatedSnd,0,2*numSample);
              dd.close();
                
    
    
            }
            void playSound(){
                AudioTrack audioTrack= null;
                try{
                audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length, AudioTrack.MODE_STATIC);
                audioTrack.write(generatedSnd, 0, generatedSnd.length);
    
    
                audioTrack.play();
                
                
                
                }
                catch(Exception e)
                {
                    System.out.print(e);
                }
        }
            
        
         
        
     }

  2. Android.net is the premier Android Forum. Registered users do not see these ads. .

  3. #2
    Super Moderator cali's Avatar
    Join Date
    Sep 2011
    Posts
    4,830
    Thanked
    329 times
    Welcome to the site. Someone should come along shortly with assistance.

Remove Ads

http://www.scramblerducati.org/

Sponsored Links

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Similar Threads

  1. Won't Boot Correctly
    By DragonMilo in forum Rescue Squad Help
    Replies: 3
    Last Post: 12-10-2013, 09:56 PM
  2. Replies: 0
    Last Post: 04-30-2011, 04:30 AM
  3. Perma-Root Created for HTC EVO Shift
    By dgstorm in forum Sprint Android News
    Replies: 0
    Last Post: 01-28-2011, 01:19 PM
  4. Perma-Root Created for HTC EVO Shift
    By dgstorm in forum Android News
    Replies: 0
    Last Post: 01-28-2011, 01:16 PM
Android Forum