forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
70 lines (57 loc) · 1.91 KB
/
Program.cs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System;
using System.Device.I2s;
using System.Diagnostics;using System.IO;
using System.Threading;
using MicrophoneIn;
using nanoFramework.Hardware.Esp32;
using nanoFramework.System.IO.FileSystem;
// This Sample will record 1s of audio input and then store it to the
// attached SD card.
Debug.WriteLine("Hello from I2S!");
// SD Card (for WAV file output):
uint cs = 5;
Configuration.SetPinFunction(23, DeviceFunction.SPI1_MOSI);
Configuration.SetPinFunction(18, DeviceFunction.SPI1_CLOCK);
Configuration.SetPinFunction(19, DeviceFunction.SPI1_MISO);
var sdCard = new SDCard(new SDCard.SDCardSpiParameters { spiBus = 1, chipSelectPin = cs });
sdCard.Mount();
// configure I2s for recording:
Configuration.SetPinFunction(0, DeviceFunction.I2S1_WS);
Configuration.SetPinFunction(34, DeviceFunction.I2S1_MDATA_IN);
I2sDevice i2s = new(new I2sConnectionSettings(1)
{
BitsPerSample = I2sBitsPerSample.Bit8,
ChannelFormat = I2sChannelFormat.OnlyLeft,
Mode = I2sMode.Master | I2sMode.Rx | I2sMode.Pdm,
CommunicationFormat = I2sCommunicationFormat.I2S,
SampleRate = 8_000
});
// should be one second of sound data:
SpanByte buff = new byte[8000];
i2s.Read(buff);
i2s.Dispose();
var header = new WavFileHeader
{
AudioFormat = 1,
SampleRate = 8000,
BitsPerSample = 8,
BytesPerSampleFrame = 1,
BytesPerSecond = 8000,
DataChunkSize = 8000,
FormatChunkSize = 16,
NumberOfChannels = 1,
FileSize = 36 + 8000
};
var outputFile = new FileStream("D:\\output.wav", FileMode.CreateNew, FileAccess.ReadWrite);
var headerData = header.GetHeaderData();
outputFile.Write(headerData, 0, headerData.Length);
var buffer = buff.ToArray();
outputFile.Write(buffer, 0, buffer.Length);
outputFile.Flush();
outputFile.Close();
outputFile.Dispose();
Thread.Sleep(Timeout.Infinite);