Skip to content

Commit

Permalink
Library Support Record And Play (#2)
Browse files Browse the repository at this point in the history
* feat: rebuild library initial commit

* chore: fixed package json

* fix: fixed android crash on stop play back

* feat: added list and clear audio files
  • Loading branch information
demchuk-alex authored Oct 23, 2024
1 parent ec69695 commit b17bce7
Show file tree
Hide file tree
Showing 37 changed files with 2,681 additions and 449 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish-package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: yarn
- run: yarn build
- run: yarn build plugin
- run: yarn npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Binary file modified .yarn/install-state.gz
Binary file not shown.
4 changes: 3 additions & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package expo.modules.audiostream

import android.util.Base64

class AudioDataEncoder {
public fun encodeToBase64(rawData: ByteArray): String {
return Base64.encodeToString(rawData, Base64.NO_WRAP)
}
}
92 changes: 92 additions & 0 deletions android/src/main/java/expo/modules/audiostream/AudioFileHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package expo.modules.audiostream

import java.io.File
import java.io.IOException
import java.io.OutputStream
import java.io.RandomAccessFile

class AudioFileHandler(private val filesDir: File) {
// Method to write WAV file header
fun writeWavHeader(out: OutputStream, sampleRateInHz: Int, channels: Int, bitDepth: Int) {
val header = ByteArray(44)
val byteRate = sampleRateInHz * channels * bitDepth / 8
val blockAlign = channels * bitDepth / 8

// RIFF/WAVE header
"RIFF".toByteArray().copyInto(header, 0)
// (file size - 8) to be updated later
header[4] = 0 // Placeholder
header[5] = 0 // Placeholder
header[6] = 0 // Placeholder
header[7] = 0 // Placeholder
"WAVE".toByteArray().copyInto(header, 8)
"fmt ".toByteArray().copyInto(header, 12)

// 16 for PCM
header[16] = 16
header[17] = 0
header[18] = 0
header[19] = 0

// PCM format ID
header[20] = 1 // Audio format 1 for PCM (not compressed)
header[21] = 0

// Number of channels
header[22] = (channels and 0xff).toByte()
header[23] = (channels shr 8 and 0xff).toByte()

// Sample rate
header[24] = (sampleRateInHz and 0xff).toByte()
header[25] = (sampleRateInHz shr 8 and 0xff).toByte()
header[26] = (sampleRateInHz shr 16 and 0xff).toByte()
header[27] = (sampleRateInHz shr 24 and 0xff).toByte()

// Byte rate
header[28] = (byteRate and 0xff).toByte()
header[29] = (byteRate shr 8 and 0xff).toByte()
header[30] = (byteRate shr 16 and 0xff).toByte()
header[31] = (byteRate shr 24 and 0xff).toByte()

// Block align
header[32] = (blockAlign and 0xff).toByte()
header[33] = (blockAlign shr 8 and 0xff).toByte()

// Bits per sample
header[34] = (bitDepth and 0xff).toByte()
header[35] = (bitDepth shr 8 and 0xff).toByte()

// Data chunk
"data".toByteArray().copyInto(header, 36)
// Data size to be updated later
header[40] = 0 // Placeholder
header[41] = 0 // Placeholder
header[42] = 0 // Placeholder
header[43] = 0 // Placeholder

out.write(header, 0, 44)
}

fun updateWavHeader(file: File) {
try {
RandomAccessFile(file, "rw").use { raf ->
val fileSize = raf.length()
val dataSize = fileSize - 44 // Subtract the header size

raf.seek(4) // Write correct file size, excluding the first 8 bytes of the RIFF header
raf.writeInt(Integer.reverseBytes((dataSize + 36).toInt()))

raf.seek(40) // Go to the data size position
raf.writeInt(Integer.reverseBytes(dataSize.toInt())) // Write the size of the data segment
}
} catch (e: IOException) {
println("Could not update WAV header: ${e.message}")
}
}

fun clearAudioStorage() {
filesDir.listFiles()?.forEach {
it.delete()
}
}
}
Loading

0 comments on commit b17bce7

Please sign in to comment.