Click here to Skip to main content
15,902,445 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an audio file which it is in the .vox format and i want to convert that into the .mp3 format through java script i already got the code for the conversion but that code is in class file means that was in back end code,but i don't want that one through java script i want to convert the file.please help me.

What I have tried:

public static void Decode(string inputFile, out string outputFile)
        {
            outputFile = String.Format("{0}\\{1}.wav", Path.GetDirectoryName(inputFile), Path.GetFileNameWithoutExtension(inputFile));
            using (FileStream inputStream = File.Open(@"C:\Users\w-887\Documents\ForUploading\31044316.vox", FileMode.Open))
            using (BinaryReader reader = new BinaryReader(inputStream))
            using (FileStream outputStream = File.Create(@"C:\Users\w-887\Documents\ForUploading\31044316.mp4"))
            using (BinaryWriter writer = new BinaryWriter(outputStream))
            {
                // Note that 32-bit integer values always take up 4 bytes.
                // Note that 16-bit integer values (shorts) always take up 2 bytes.
                // Note that HEX values resolve as 32-bit integers unless casted as something else, such as short values.
                // ChunkID: "RIFF"
                writer.Write(0x46464952);
                // ChunkSize: The size of the entire file in bytes minus 8 bytes for the two fields not included in this count: ChunkID and ChunkSize.
                writer.Write((int)(reader.BaseStream.Length * 4) + 36);
                // Format: "WAVE"
                writer.Write(0x45564157);
                // Subchunk1ID: "fmt " (with the space).
                writer.Write(0x20746D66);
                // Subchunk1Size: 16 for PCM.
                writer.Write(16);
                // AudioFormat: 1 for PCM.
                writer.Write((short)1);
                // NumChannels: 1 for Mono. 2 for Stereo.
                writer.Write((short)1);
                // SampleRate: 8000 is usually the default for VOX.
                writer.Write(8000);
                // ByteRate: SampleRate * NumChannels * BitsPerSample / 8.
                writer.Write(12000);
                // BlockAlign: NumChannels * BitsPerSample / 8. I rounded this up to 2. It sounds best this way.
                writer.Write((short)2);
                // BitsPerSample: I will set this as 12 (12 bits per raw output sample as per the VOX specification).
                writer.Write((short)12);
                // Subchunk2ID: "data"
                writer.Write(0x61746164);
                // Subchunk2Size: NumSamples * NumChannels * BitsPerSample / 8. You can also think of this as the size of the read of the subchunk following this number.
                writer.Write((int)(reader.BaseStream.Length * 4));
                // Write the data stream to the file in linear audio.
                while (reader.BaseStream.Position != reader.BaseStream.Length)
                {
                    byte b = reader.ReadByte();
                    float firstDifference = GetDifference((byte)(b / 16));
                    signal += firstDifference;
                    writer.Write(TruncateSignalIfNeeded());
                    float secondDifference = GetDifference((byte)(b % 16));
                    signal += secondDifference;
                    writer.Write(TruncateSignalIfNeeded());
                }
            }
                       //         Response.ContentType = ContentType;
                      //Response.AppendHeader("Content-Disposition", ("attachment; filename=" + Path.GetFileName(filePath)));
                     //Response.WriteFile(filePath);
                    //Response.End()
        }

        static short TruncateSignalIfNeeded()
        {
            // Keep signal truncated to 12 bits since, as per the VOX spec, each 4 bit input has 12 output bits.
            // Note that 12 bits is 0b111111111111. That's 0xFFF in HEX. That's also 4095 in decimal.
            // The sound wave is a signed signal, so factoring in 1 unused bit for the sign, that's 4095/2 rounded down to 2047.
            if (signal > 2047)
            {
                signal = 2047;
            }
            if (signal < -2047)
            {
                signal = -2047;
            }
            return (short)signal;
        }

        static float GetDifference(byte nibble)
        {
            int stepSize = GetNextStepSize(nibble);
            float difference = ((stepSize * GetBit(nibble, 2)) + ((stepSize / 2) * GetBit(nibble, 1)) + (stepSize / 4 * GetBit(nibble, 0)) + (stepSize / 8));
            if (GetBit(nibble, 3) == 1)
            {
                difference = -difference;
            }
            return difference;
        }

        static byte GetBit(byte b, int zeroBasedBitNumber)
        {
            // Shift the bits to the right by the number of the bit you want to get and then logic AND it with 1 to clear bits trailing to the left of your desired bit. 
            return (byte)((b >> zeroBasedBitNumber) & 1);
        }

        static int GetNextStepSize(byte nibble)
        {
            if (!computedNextStepSizeOnce)
            {
                computedNextStepSizeOnce = true;
                return possibleStepSizes[0];
            }
            else
            {
                int magnitude = GetMagnitude(nibble);
                if (previousStepSizeIndex + magnitude > 48)
                {
                    previousStepSizeIndex = previousStepSizeIndex + magnitude;
                    return possibleStepSizes[48];
                }
                else if (previousStepSizeIndex + magnitude > 0)
                {
                    previousStepSizeIndex = previousStepSizeIndex + magnitude;
                    return possibleStepSizes[previousStepSizeIndex];
                }
                else
                {
                    return possibleStepSizes[0];
                }
            }
        }

        static int GetMagnitude(byte nibble)
        {
            if (nibble == 15 || nibble == 7)
                return 8;
            else if (nibble == 14 || nibble == 6)
                return 6;
            else if (nibble == 13 || nibble == 5)
                return 4;
            else if (nibble == 12 || nibble == 4)
                return 2;
            else
                return -1;

        }
Posted
Updated 22-Nov-16 2:31am
Comments
Mehdi Gholam 22-Nov-16 8:22am    
Javascript is very limited why do you want to do the conversion in javascript?

1 solution

JavaScript? In browser? Impossible...
When JavaScript runs as part of a web page (in the browser) it has no access whatsoever to the OS for security reasons. So you can't even open a file from the local storage...
 
Share this answer
 
Comments
F-ES Sitecore 22-Nov-16 8:40am    
Agreed, only real way would be to submit the file to a webmethod\api and have that convert the file then stream it back to the client.
Member 12727089 23-Nov-16 0:27am    
No i am getting the file name at browser, after getting the filename which is in the format of the .vox format how i can convert this .vox file into the .mp3 format

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900