forked from gkonovalov/android-vad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added instrumented test for VadSilero
- Loading branch information
1 parent
3862f2e
commit 0618842
Showing
3 changed files
with
80 additions
and
22 deletions.
There are no files selected for viewing
Binary file not shown.
22 changes: 0 additions & 22 deletions
22
silero/src/androidTest/java/com/konovalov/vad/silero/ExampleInstrumentedTest.kt
This file was deleted.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
silero/src/androidTest/java/com/konovalov/vad/silero/VadSileroTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.konovalov.silero | ||
|
||
import android.content.Context | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.konovalov.vad.silero.Vad | ||
import com.konovalov.vad.silero.VadSilero | ||
import com.konovalov.vad.silero.config.FrameSize | ||
import com.konovalov.vad.silero.config.Mode | ||
import com.konovalov.vad.silero.config.SampleRate | ||
import junit.framework.TestCase.assertEquals | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
|
||
/** | ||
* Created by Georgiy Konovalov on 12/22/2023. | ||
* <p> | ||
* Instrumented test for VadSilero class. | ||
* </p> | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class VadSileroTest { | ||
|
||
private lateinit var vad: VadSilero | ||
private lateinit var testContext: Context | ||
|
||
@Before | ||
fun setUp() { | ||
this.testContext = InstrumentationRegistry.getInstrumentation().context | ||
|
||
this.vad = Vad.builder() | ||
.setContext(testContext) | ||
.setSampleRate(SampleRate.SAMPLE_RATE_16K) | ||
.setFrameSize(FrameSize.FRAME_SIZE_512) | ||
.setMode(Mode.VERY_AGGRESSIVE) | ||
.build() | ||
} | ||
|
||
@After | ||
fun shutdown() { | ||
vad.close() | ||
} | ||
|
||
@Test | ||
fun testIsSpeech() { | ||
// List for VAD results | ||
val actualResult = mutableListOf<Boolean>() | ||
|
||
// List with expected VAD results | ||
val expectedResult = listOf( | ||
false, false, false, false, false, | ||
false, true, true, true, true, | ||
true, true, true, true, true, | ||
true, true, true, false, false | ||
) | ||
|
||
// Buffer size should be 2x for ByteArray | ||
val chunkSize = vad.frameSize.value * 2 | ||
|
||
// Reading audio data from test file | ||
testContext.assets.open("hello.wav").buffered().use { input -> | ||
// Skip WAV Header | ||
input.skip(44) | ||
|
||
while (input.available() > 0) { | ||
// Read audio Frame | ||
val frameChunk = ByteArray(chunkSize).apply { input.read(this) } | ||
|
||
// Save interference result | ||
actualResult.add(vad.isSpeech(frameChunk)) | ||
} | ||
} | ||
|
||
// Compare expectedResult with actualResult | ||
assertEquals(expectedResult, actualResult) | ||
} | ||
} |