Skip to content

Commit 7285a5d

Browse files
authored
Merge pull request #17 from neocortex-link/feat/audio-trim-improvement
Audio Trimming Improvements
2 parents 2ac1332 + b5279e4 commit 7285a5d

File tree

6 files changed

+156
-117
lines changed

6 files changed

+156
-117
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.3.7] - 2 April 2025
8+
- Audio trimming improved
9+
710
## [0.3.6] - 28 March 2025
811
- Microphone Permission utility for mobile builds
912
- Mobile Audio Chat Test sample

Runtime/Extension Methods/AudioClipExtensions.cs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,63 @@
22
using System.IO;
33
using UnityEngine;
44
using System.Collections.Generic;
5+
using System.Linq;
56

67
namespace Neocortex
78
{
89
public static class AudioClipExtensions
910
{
1011
private const int HeaderSize = 44;
11-
12-
public static AudioClip Trim(this AudioClip audioClip, float treshold = 0.002f)
12+
13+
public static AudioClip Trim(this AudioClip audioClip, float threshold = 0.001f)
1314
{
1415
float[] samples = new float[audioClip.samples];
1516
audioClip.GetData(samples, 0);
16-
List<float> sampleList = new List<float>(samples);
17+
List<float> outputSamples = new List<float>();
1718

18-
sampleList.RemoveAll(sample => Mathf.Abs(sample) < treshold);
19+
int silenceMinSamples = Mathf.RoundToInt(audioClip.frequency * 0.2f); // 0.2 seconds
1920

20-
// if audio is shorter than 0.2 seconds, return null
21-
// this is to prevent sound such as mouse clicks from being sent
22-
if (sampleList.Count < audioClip.frequency / 4)
21+
int i = 0;
22+
while (i < samples.Length)
2323
{
24-
return null;
24+
if (Mathf.Abs(samples[i]) < threshold)
25+
{
26+
int silentStart = i;
27+
28+
while (i < samples.Length && Mathf.Abs(samples[i]) < threshold)
29+
{
30+
i++;
31+
}
32+
33+
int silentLength = i - silentStart;
34+
if (silentLength < silenceMinSamples)
35+
{
36+
for (int j = silentStart; j < i; j++)
37+
{
38+
outputSamples.Add(samples[j]);
39+
}
40+
}
41+
}
42+
else
43+
{
44+
outputSamples.Add(samples[i]);
45+
i++;
46+
}
2547
}
2648

27-
if (sampleList.Count > 0)
49+
// Remove 0.2 or shorter loud segments, could mouse click etc
50+
if (outputSamples.Count <= silenceMinSamples)
2851
{
29-
var lengthSamples = Mathf.Max(sampleList.Count, audioClip.frequency);
30-
var tempClip = AudioClip.Create("TempClip", lengthSamples, audioClip.channels, audioClip.frequency, false);
31-
tempClip.SetData(sampleList.ToArray(), 0);
32-
33-
return tempClip;
52+
return null;
3453
}
3554

36-
return null;
37-
}
55+
int lengthSamples = Mathf.Max(outputSamples.Count, audioClip.frequency);
56+
AudioClip tempClip = AudioClip.Create("TempClip", lengthSamples, audioClip.channels, audioClip.frequency, false);
57+
tempClip.SetData(outputSamples.ToArray(), 0);
3858

59+
return tempClip;
60+
}
61+
3962
public static byte[] EncodeToWav(this AudioClip clip)
4063
{
4164
return Encode(clip);
@@ -47,6 +70,9 @@ private static byte[] Encode(AudioClip clip)
4770
Convert(memoryStream, clip);
4871
WriteHeader(memoryStream, clip);
4972
byte[] bytes = memoryStream.GetBuffer();
73+
74+
// Check out the result for audio quality
75+
// File.WriteAllBytes(Application.persistentDataPath + "/input.wav", bytes);
5076

5177
return bytes;
5278
}

Runtime/NeocortexWebAudioReceiver.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public void LogWrittenBuffer(int written)
5555
// called from JS
5656
public void NotifyRecordingChange(int newRecordingState)
5757
{
58-
Debug.Log("NotifyRecordingChange: " + newRecordingState);
59-
6058
if((int)microphoneState == newRecordingState) return;
6159

6260
MicrophoneState oldState = microphoneState;

0 commit comments

Comments
 (0)