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

    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:

    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;
     protected void onCreate(Bundle savedInstanceState) {  
    Thread thread=new Thread(new Runnable(){
        public void run(){
            try {
            } catch (IOException e) {
                // TODO Auto-generated catch block                                                                                                                                                                                          
            handler.post(new Runnable(){
                public void run(){
    protected void onResume()
        void genTone() throws IOException{
            double instfreq=0, numerator;
            for (int i=0;i<numSample; i++ )
                 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));
             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.writeInt(0); // Final file size not known yet, write 0 
              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.writeInt(0); // Data chunk size not known yet, write 0
            void playSound(){
                AudioTrack audioTrack= null;
                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);
                catch(Exception 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
    329 times
    Welcome to the site. Someone should come along shortly with assistance.

Remove Ads


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