1 people like it.

ReadWavFile.fsx

Function to read a wav file

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
type SoundFile (samplingRate: float, 
              numChannels: int, 
              data: (float list * float list),
              isPCM: bool) =
  member f.SamplingRate = samplingRate
  member f.Data = data
  member f.IsPCM = isPCM
  member f.NumChannels = numChannels

/// <summary>Reads a PCM wave file and return a SoundFile object</summary>
/// <param name="path">Path of the file</param>
/// <returns>a SoundFile object representing the data in the wav file. If the
/// file has only one channel, then the first of the tuple in Data contains
/// the data and the second of the tuple is empty</returns>
let readWavFile (path: string) =
  use fileStream = new System.IO.FileStream( path, System.IO.FileMode.Open )
  use reader = new System.IO.BinaryReader(fileStream)
  // read chunk id - 4 bytes
  let id = reader.ReadBytes(4)
  if id <> "RIFF"B then
    failwith "Not a WAV file"
  // ignore next 12 bytes - chunk size: 4, format: 4, subChunkId: 4
  reader.ReadBytes(12) |> ignore

  let sizeChunk1 = System.BitConverter.ToInt32(reader.ReadBytes(4), 0)
  let audioFormat = int(System.BitConverter.ToInt16(reader.ReadBytes(2), 0))
  let numChannels = int(System.BitConverter.ToInt16(reader.ReadBytes(2), 0))
  let samplingRate = System.BitConverter.ToInt32(reader.ReadBytes(4), 0)

  // ignore rest of chunk 1
  reader.ReadBytes(sizeChunk1-8) |> ignore

  if int audioFormat <> 1 then
    // Non-PCM
    reader.ReadBytes(4) |> ignore
    let nExtraBytes = System.BitConverter.ToInt32(reader.ReadBytes(4), 0)
    reader.ReadBytes(nExtraBytes) |> ignore

  let idChunk2 = reader.ReadBytes(4)
  if idChunk2 <> "data"B then
    failwith "Incorrect format: chunk 2 id not 'data'"
  let nBytes = System.BitConverter.ToInt32(reader.ReadBytes(4), 0)  
  let data = 
    let raw = reader.ReadBytes(nBytes)
    if numChannels = 1 then 
      (Array.map (fun x -> float x) raw |> Array.toList, [])
    else
      Array.foldBack (fun x (l,r) -> ((float x)::r, l)) raw ([],[])

  SoundFile (float samplingRate, numChannels, data, (audioFormat = 1))
Multiple items
type SoundFile =
  new : samplingRate:float * numChannels:int * data:(float list * float list) * isPCM:bool -> SoundFile
  member Data : float list * float list
  member IsPCM : bool
  member NumChannels : int
  member SamplingRate : float

Full name: Script.SoundFile

--------------------
new : samplingRate:float * numChannels:int * data:(float list * float list) * isPCM:bool -> SoundFile
val samplingRate : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val numChannels : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val data : float list * float list
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val isPCM : bool
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
val f : SoundFile
member SoundFile.SamplingRate : float

Full name: Script.SoundFile.SamplingRate
Multiple items
member SoundFile.Data : float list * float list

Full name: Script.SoundFile.Data

--------------------
namespace Microsoft.FSharp.Data
member SoundFile.IsPCM : bool

Full name: Script.SoundFile.IsPCM
member SoundFile.NumChannels : int

Full name: Script.SoundFile.NumChannels
val readWavFile : path:string -> SoundFile

Full name: Script.readWavFile


 <summary>Reads a PCM wave file and return a SoundFile object</summary>
 <param name="path">Path of the file</param>
 <returns>a SoundFile object representing the data in the wav file. If the
 file has only one channel, then the first of the tuple in Data contains
 the data and the second of the tuple is empty</returns>
val path : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val fileStream : System.IO.FileStream
namespace System
namespace System.IO
Multiple items
type FileStream =
  inherit Stream
  new : path:string * mode:FileMode -> FileStream + 14 overloads
  member BeginRead : array:byte[] * offset:int * numBytes:int * userCallback:AsyncCallback * stateObject:obj -> IAsyncResult
  member BeginWrite : array:byte[] * offset:int * numBytes:int * userCallback:AsyncCallback * stateObject:obj -> IAsyncResult
  member CanRead : bool
  member CanSeek : bool
  member CanWrite : bool
  member EndRead : asyncResult:IAsyncResult -> int
  member EndWrite : asyncResult:IAsyncResult -> unit
  member Flush : unit -> unit + 1 overload
  member GetAccessControl : unit -> FileSecurity
  ...

Full name: System.IO.FileStream

--------------------
System.IO.FileStream(path: string, mode: System.IO.FileMode) : unit
   (+0 other overloads)
System.IO.FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: System.IO.FileAccess) : unit
   (+0 other overloads)
System.IO.FileStream(path: string, mode: System.IO.FileMode, access: System.IO.FileAccess) : unit
   (+0 other overloads)
System.IO.FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: System.IO.FileAccess, bufferSize: int) : unit
   (+0 other overloads)
System.IO.FileStream(path: string, mode: System.IO.FileMode, access: System.IO.FileAccess, share: System.IO.FileShare) : unit
   (+0 other overloads)
System.IO.FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: System.IO.FileAccess, bufferSize: int, isAsync: bool) : unit
   (+0 other overloads)
System.IO.FileStream(path: string, mode: System.IO.FileMode, access: System.IO.FileAccess, share: System.IO.FileShare, bufferSize: int) : unit
   (+0 other overloads)
System.IO.FileStream(path: string, mode: System.IO.FileMode, access: System.IO.FileAccess, share: System.IO.FileShare, bufferSize: int, options: System.IO.FileOptions) : unit
   (+0 other overloads)
System.IO.FileStream(path: string, mode: System.IO.FileMode, access: System.IO.FileAccess, share: System.IO.FileShare, bufferSize: int, useAsync: bool) : unit
   (+0 other overloads)
System.IO.FileStream(path: string, mode: System.IO.FileMode, rights: System.Security.AccessControl.FileSystemRights, share: System.IO.FileShare, bufferSize: int, options: System.IO.FileOptions) : unit
   (+0 other overloads)
type FileMode =
  | CreateNew = 1
  | Create = 2
  | Open = 3
  | OpenOrCreate = 4
  | Truncate = 5
  | Append = 6

Full name: System.IO.FileMode
field System.IO.FileMode.Open = 3
val reader : System.IO.BinaryReader
Multiple items
type BinaryReader =
  new : input:Stream -> BinaryReader + 1 overload
  member BaseStream : Stream
  member Close : unit -> unit
  member Dispose : unit -> unit
  member PeekChar : unit -> int
  member Read : unit -> int + 2 overloads
  member ReadBoolean : unit -> bool
  member ReadByte : unit -> byte
  member ReadBytes : count:int -> byte[]
  member ReadChar : unit -> char
  ...

Full name: System.IO.BinaryReader

--------------------
System.IO.BinaryReader(input: System.IO.Stream) : unit
System.IO.BinaryReader(input: System.IO.Stream, encoding: System.Text.Encoding) : unit
val id : byte []
System.IO.BinaryReader.ReadBytes(count: int) : byte []
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val sizeChunk1 : int
type BitConverter =
  static val IsLittleEndian : bool
  static member DoubleToInt64Bits : value:float -> int64
  static member GetBytes : value:bool -> byte[] + 9 overloads
  static member Int64BitsToDouble : value:int64 -> float
  static member ToBoolean : value:byte[] * startIndex:int -> bool
  static member ToChar : value:byte[] * startIndex:int -> char
  static member ToDouble : value:byte[] * startIndex:int -> float
  static member ToInt16 : value:byte[] * startIndex:int -> int16
  static member ToInt32 : value:byte[] * startIndex:int -> int
  static member ToInt64 : value:byte[] * startIndex:int -> int64
  ...

Full name: System.BitConverter
System.BitConverter.ToInt32(value: byte [], startIndex: int) : int
val audioFormat : int
System.BitConverter.ToInt16(value: byte [], startIndex: int) : int16
val samplingRate : int
val nExtraBytes : int
val idChunk2 : byte []
val nBytes : int
val raw : byte []
module Array

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val x : byte
val toList : array:'T [] -> 'T list

Full name: Microsoft.FSharp.Collections.Array.toList
val foldBack : folder:('T -> 'State -> 'State) -> array:'T [] -> state:'State -> 'State

Full name: Microsoft.FSharp.Collections.Array.foldBack
val l : float list
val r : float list

More information

Link:http://fssnip.net/sa
Posted:4 years ago
Author:albertp007
Tags: #soundfile #wavfile #fsharp , tryfsharp