Exemplo n.º 1
0
    public static void WriteWaveFile(BinaryFile wavfile, double[][] sound, int channels, int samplecount, int samplerate, int format_param)
    {
        int i = 0;

        int[] tag = { 1179011410, 0, 1163280727, 544501094, 16, 1, 1, 0, 0, 0, 0, 1635017060, 0, 0 };

                #if DEBUG
        Console.Write("WriteWaveFile...\n");
                #endif

        //********WAV tags generation********

        tag[12] = samplecount * (format_param / 8) * channels;
        tag[1]  = tag[12] + 36;
        tag[7]  = samplerate;
        tag[8]  = samplerate * format_param / 8;
        tag[9]  = format_param / 8;
        tag[6]  = channels;
        tag[10] = format_param;

        if ((format_param == 8) || (format_param == 16))
        {
            tag[5] = 1;
        }
        if (format_param == 32)
        {
            tag[5] = 3;
        }
        //--------WAV tags generation--------

        // tag writing
        for (i = 0; i < 13; i++)
        {
            if ((i == 5) || (i == 6) || (i == 9) || (i == 10))
            {
                GlobalMembersUtil.WriteUInt16((ushort)tag[i], wavfile);
            }
            else
            {
                GlobalMembersUtil.WriteUInt32((uint)tag[i], wavfile);
            }
        }

        if (format_param == 8)
        {
            Write8Bit(wavfile, sound, samplecount, channels);
        }
        if (format_param == 16)
        {
            Write16Bit(wavfile, sound, samplecount, channels);
        }
        if (format_param == 32)
        {
            Write32BitFloat(wavfile, sound, samplecount, channels);
        }

        wavfile.Close();
    }
Exemplo n.º 2
0
    public static void BMPWrite(BinaryFile bmpfile, double[][] image, int y, int x)
    {
        int    i         = 0; // various iterators
        int    iy        = 0;
        int    ix        = 0;
        int    ic        = 0;
        int    filesize  = 0;
        int    imagesize = 0;
        byte   zerobytes = new byte();
        byte   val       = new byte();
        byte   zero      = 0;
        double vald;

                #if DEBUG
        Console.Write("BMPWrite...\n");
                #endif

        zerobytes = (byte)(4 - ((x * 3) & 3));         // computation of zero bytes
        if (zerobytes == 4)
        {
            zerobytes = 0;
        }

        //********Tags********
        filesize  = 56 + ((x * 3) + zerobytes) * y;
        imagesize = 2 + ((x * 3) + zerobytes) * y;

        GlobalMembersUtil.WriteUInt16(19778, bmpfile);
        GlobalMembersUtil.WriteUInt32((UInt32)filesize, bmpfile);
        GlobalMembersUtil.WriteUInt32(0, bmpfile);
        GlobalMembersUtil.WriteUInt32(54, bmpfile);
        GlobalMembersUtil.WriteUInt32(40, bmpfile);
        GlobalMembersUtil.WriteUInt32((UInt32)x, bmpfile);
        GlobalMembersUtil.WriteUInt32((UInt32)y, bmpfile);
        GlobalMembersUtil.WriteUInt16(1, bmpfile);
        GlobalMembersUtil.WriteUInt32(24, bmpfile);
        GlobalMembersUtil.WriteUInt16(0, bmpfile);
        GlobalMembersUtil.WriteUInt32((UInt32)imagesize, bmpfile);
        GlobalMembersUtil.WriteUInt32(2834, bmpfile);
        GlobalMembersUtil.WriteUInt32(2834, bmpfile);
        GlobalMembersUtil.WriteUInt32(0, bmpfile);
        GlobalMembersUtil.WriteUInt32(0, bmpfile);
        //--------Tags--------

        for (iy = y - 1; iy != -1; iy--)       // backwards writing
        {
            for (ix = 0; ix < x; ix++)
            {
                // define color
                vald = image[iy][ix] * 255.0;

                if (vald > 255.0)
                {
                    vald = 255.0;
                }

                if (vald < 0.0)
                {
                    vald = 0.0;
                }

                val = (byte)vald;

                for (ic = 2; ic != -1; ic--)
                {
                    bmpfile.Write(val);
                }
            }

            // write padding bytes
            for (i = 0; i < zerobytes; i++)
            {
                bmpfile.Write(zero);
            }
        }

        GlobalMembersUtil.WriteUInt16(0, bmpfile);

        bmpfile.Close();

                #if DEBUG
        Console.Write("Image size : {0:D}x{1:D}\n", x, y);
                #endif
    }