Skip to content

Commit

Permalink
chore: split encoder and decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
RTAkland committed Nov 17, 2024
1 parent be055d7 commit 91f5fa6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/11/17
*/

@file:Suppress("unused")

package cn.rtast.rob.silk

import io.github.kasukusakura.silkcodec.SilkCoder
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.InputStream

/**
* 从一个[InputStream]转换成PCM格式的音频, 但是需要参数传入
*/
fun InputStream.decodeToSilk(sampleRate: Int, strict: Boolean, loss: Int): ByteArray {
val outputStream = ByteArrayOutputStream()
SilkCoder.decode(this, outputStream, strict, sampleRate, loss)
return outputStream.toByteArray()
}

/**
* 从一个[File]转换成PCM格式的音频, 但是需要参数传入
*/
fun File.decodeToSilk(sampleRate: Int, strict: Boolean, loss: Int): ByteArray {
val outputStream = ByteArrayOutputStream()
SilkCoder.decode(this.inputStream(), outputStream, strict, sampleRate, loss)
return outputStream.toByteArray()
}

/**
* 从一个[InputStream]转换成PCM格式的音频
*/
fun InputStream.decodeToSilk(): ByteArray {
val outputStream = ByteArrayOutputStream()
SilkCoder.decode(this, outputStream)
return outputStream.toByteArray()
}

/**
* 从一个[File]对象转换成PCM格式的音频
*/
fun File.decodeToSilk(): ByteArray {
val outputStream = ByteArrayOutputStream()
SilkCoder.decode(this.inputStream(), outputStream)
return outputStream.toByteArray()
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,3 @@ fun File.encodeToSilk(sampleRate: Int): ByteArray {
SilkCoder.encode(this.inputStream(), outputStream, sampleRate)
return outputStream.toByteArray()
}

/**
* 从一个[InputStream]转换成PCM格式的音频, 但是需要参数传入
*/
fun InputStream.decodeToSilk(sampleRate: Int, strict: Boolean, loss: Int): ByteArray {
val outputStream = ByteArrayOutputStream()
SilkCoder.decode(this, outputStream, strict, sampleRate, loss)
return outputStream.toByteArray()
}

/**
* 从一个[File]转换成PCM格式的音频, 但是需要参数传入
*/
fun File.decodeToSilk(sampleRate: Int, strict: Boolean, loss: Int): ByteArray {
val outputStream = ByteArrayOutputStream()
SilkCoder.decode(this.inputStream(), outputStream, strict, sampleRate, loss)
return outputStream.toByteArray()
}

/**
* 从一个[InputStream]转换成PCM格式的音频
*/
fun InputStream.decodeToSilk(): ByteArray {
val outputStream = ByteArrayOutputStream()
SilkCoder.decode(this, outputStream)
return outputStream.toByteArray()
}

/**
* 从一个[File]对象转换成PCM格式的音频
*/
fun File.decodeToSilk(): ByteArray {
val outputStream = ByteArrayOutputStream()
SilkCoder.decode(this.inputStream(), outputStream)
return outputStream.toByteArray()
}

0 comments on commit 91f5fa6

Please sign in to comment.