Jan 04

前陣子,在工作上剛好需要對 wav 檔案格式進行判定,可是 NAudio 製作出來的格式用來作判斷又有錯誤,所以土法煉鋼寫了一個小 class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    public class Wave
    {
        public struct Format
        {
            public String FileName = String.Empty;
            public Boolean PCM = false;
            public uint Channel = 0;
            public uint SampleRate = 0;
            public uint BitsPerSample = 0;
            public uint ByteRate = 0;
            public uint BlockAlign = 0;
        };

        private Format _fmt;

        private void fillUp()
        {
            _fmt.ByteRate = _fmt.Channel * _fmt.SampleRate * _fmt.BitsPerSample / 8;
            _fmt.BlockAlign = _fmt.Channel * _fmt.BitsPerSample / 8;
        }

        public Format getType(String fileName)
        {
            _fmt.FileName = fileName;

            try
            {
                FileStream fs = File.OpenRead(@fileName);
                Byte[] readTmp = new Byte[36];

                fs.Read(readTmp, 0, 36);
                fs.close();

                if (BitConverter.ToUInt16(readTmp, 20) == 1)
                {
                    _fmt.PCM = true;
                }

                _fmt.Channel = BitConverter.ToUInt16(readTmp, 22);
                _fmt.SampleRate = BitConverter.ToUInt32(readTmp, 24);
                _fmt.BitsPerSample = BitConverter.ToUInt16(readTmp, 34);

                fillUp();
            }
            catch (Exception e)
            {
                Console.WriteLine("Wave.getType(fileName) Error : " + e.ToString());
            }
            return _fmt;
        }
    }
  • 檢查 FileName 是否為空字串,就知道是否成功呼叫 getType() 。
  • 檢查 PCM 是否為 true,就知道該檔案是不是 PCM 格式的 wav 檔。
  • wav 檔播放的時間會等於 ByteRate。

Technorati Tags: , , , , , ,

Tags: , , , , , ,
(Visited 1779 times)
Jun 22

剛剛弄出來的東西.

DsExport.vb :

Public Class DsExport

  Public Shared Function Export(ByVal ds As DataSet, _
                                ByVal dtName As String, _
                                ByVal colList() As String, _
                                ByVal colValue() As Integer) As String
    Dim header As String = ""
    Dim body As String = ""
    Dim record As String = ""

    For Each col As String In colList
      header = header & Chr(34) & col & Chr(34) & ","
    Next
      header = header.Substring(0, header.Length - 1)

    For Each row As DataRow In ds.Tables(dtName).Rows
      Dim arr() As Object = row.ItemArray()

      For Each i As Integer In colValue
        If arr(i).ToString().IndexOf(",") > 0 Then
          record = record & Chr(34) & arr(i).ToString() & Chr(34) & ","
        Else
          record = record & arr(i).ToString() & ","
        End If
      Next

      body = body & record.Substring(0, record.Length - 1) & vbCrLf
      record = ""
    Next

    Return header & vbCrLf & body

  End Function

End Class

呼叫方式:

  Private Sub BtnExport_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles BtnExport.Click
    Dim colList() As String = {"CSV表頭1", "CSV表頭2", "CSV表頭3"}

    "顯示順序: SQL query 刮出來的欄位0, 4, 2
    Dim colValue() As Integer = {0, 4, 2}

    Dim strData As String = _
      DsExport.Export(DataSet 變數名稱, _
                      "DataSet 變數中的 DataTabel 名稱", _
                      colList, colValue)

    Dim binData() As Byte = System.Text.Encoding.Default.GetBytes(strData)

    Response.Clear()
    Response.AddHeader("Content-Type", "application/vnd.ms-access")
    Response.AddHeader("Content-Disposition", "inline;filename=檔案名稱.csv")
    Response.BinaryWrite(binData)
    Response.End()
  End Sub

Technorati Tags: , , ,

Tags: , , ,
(Visited 6893 times)