NCKitFileProcessor
Offline file denoise. Input: decodable audio/video file. Output: 16-bit PCM WAV at 48 kHz mono.
APIโ
public enum NCKitFileProcessor {
public static let targetSampleRate: Double // 48_000
public static func processFile(
inputURL: URL,
outputURL: URL,
processor: NCKitProcessor
) throws
}
object NCKitFileProcessor {
const val TARGET_SAMPLE_RATE: Int = 48_000
const val DEFAULT_THREADS: Int = 2
fun recommendedThreads(): Int
@Throws(NCKitException::class)
fun processFile(
inputFile: File,
outputFile: File,
processor: NCKitProcessor,
)
@Throws(NCKitException::class)
fun processFile(
inputFile: File,
outputFile: File,
processor: NCKitProcessor,
threads: Int,
polish: Boolean,
)
@Throws(NCKitException::class)
fun processPcm(
pcm48kMono: FloatArray,
outputFile: File,
processor: NCKitProcessor,
threads: Int,
polish: Boolean,
)
@Throws(NCKitException::class)
fun processPcm(
pcm48kMono: FloatArray,
processor: NCKitProcessor,
threads: Int,
polish: Boolean,
sink: NCKitOrderedSink,
)
@Throws(NCKitException::class)
fun processPcm(
interleavedPcm: ShortArray,
sampleRate: Int,
channelCount: Int,
outputFile: File,
processor: NCKitProcessor,
threads: Int = DEFAULT_THREADS,
polish: Boolean = true,
)
}
fun interface NCKitOrderedSink {
fun onSamples(samples: FloatArray, from: Int, until: Int)
}
Usageโ
import NCKit
let modelURL = try NCKitModelLocator.modelTarGzURL()
let processor = try NCKitProcessor(modelURL: modelURL)
try NCKitFileProcessor.processFile(
inputURL: noisyFileURL,
outputURL: cleanFileURL,
processor: processor
)
import com.fiveexceptions.nckit.NCKitFileProcessor
import com.fiveexceptions.nckit.NCKitModelLocator
import com.fiveexceptions.nckit.NCKitProcessor
val modelFile = NCKitModelLocator.modelFile(context)
val processor = NCKitProcessor(modelFile)
NCKitFileProcessor.processFile(noisyFile, cleanFile, processor)
Batch (reuse processor)โ
let modelURL = try NCKitModelLocator.modelTarGzURL()
let processor = try NCKitProcessor(modelURL: modelURL)
for (input, output) in jobs {
try NCKitFileProcessor.processFile(
inputURL: input,
outputURL: output,
processor: processor
)
}
val modelFile = NCKitModelLocator.modelFile(context)
val processor = NCKitProcessor(modelFile)
for ((input, output) in jobs) {
NCKitFileProcessor.processFile(input, output, processor)
}
Background threadโ
try await Task.detached(priority: .userInitiated) {
try NCKitFileProcessor.processFile(
inputURL: inputURL,
outputURL: outputURL,
processor: processor
)
}.value
withContext(Dispatchers.IO) {
NCKitFileProcessor.processFile(inputFile, outputFile, processor)
}
Threads, Gold polish, and raw PCMโ
The three-argument processFile uses DEFAULT_THREADS (2) and Gold polish. recommendedThreads() returns 1โ4 from the device's fast cores. Pass that value when you want the phone to choose:
NCKitFileProcessor.processFile(
inputFile,
outputFile,
processor,
threads = NCKitFileProcessor.recommendedThreads(),
polish = true,
)
Files up to about ten minutes can be denoised in memory on more than one engine. Longer files stream, so they are not held twice in RAM. Each worker is its own engine. The 30 ms model delay (frameLength * 3) is dropped before the WAV is written.
processPcm is the same denoise for PCM you already have, such as microphone audio kept during a recording. Float input must already be 48 kHz mono. Short input is converted to that layout. The sink form delivers samples in order while later audio is still denoising. Finish with samples[from until until] before onSamples returns, because the array is reused.
NCKitFileProcessor.processPcm(pcm48kMono, cleanWav, processor)
NCKitFileProcessor.processPcm(
pcm48kMono,
processor,
threads = NCKitFileProcessor.recommendedThreads(),
polish = true,
) { samples, from, until ->
encoder.write(samples, from, until)
}
Video calls go through NCKitVideoProcessor. It uses this PCM path, then writes AAC into the MP4.
processFile and processPcm are blocking. Call them on a background thread.
Throwsโ
| Error | When |
|---|---|
cannotOpenInput / CannotOpenInput | Source file cannot be read |
cannotCreateOutput / CannotCreateOutput | Output path cannot be written |
unsupportedFormat / UnsupportedFormat | No decodable audio |
resampleFailed / ResampleFailed | Resample to 48 kHz failed |