-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainActivity.kt
More file actions
135 lines (102 loc) · 3.99 KB
/
MainActivity.kt
File metadata and controls
135 lines (102 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.example.fishe.project_cs3218
import android.Manifest
import android.content.pm.PackageManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private val RECORD_AUDIO_REQUEST_CODE = 101
lateinit private var soundSampler: SoundReceiver
lateinit private var soundTransmitter: SoundTransmitter
companion object {
lateinit var buffer: ShortArray
lateinit var soundFFTMag: DoubleArray
var FFT_Len = 1024
var bufferSize: Int = 0 // in bytes, will be altered in SoundReceiver.ktt
const val FS = 44100 // sampling frequency
var mx = -99999.0
var freqResolution: Double = FS * 1.0 / FFT_Len
const val offset = 21 //lower frequency is not used for coded because noises usually are in low frequencies
const val bytePerSoundSignal = 16
const val freqResolutionPerByte = 25
var threshold = 9999.9
var message: String = "hi"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// seek permission to do audio recording
setupPermissions()
initiateSoundTransmitting()
sendButton?.setOnClickListener {
val x: String? = textMessage?.text.toString()
val charset = Charsets.UTF_8
if (x.isNullOrBlank()) {
textMessage?.error = "Empty Message"
textMessage?.requestFocus()
return@setOnClickListener
}
textMessage!!.text.clear()
val byteArray = x?.toByteArray(charset)
Toast.makeText(this, byteArray?.contentToString() , Toast.LENGTH_LONG)
.show()
//textView.text = byteArray?.toString(charset)
textView.text = message
soundTransmitter.playSound(byteArray)
}
}
override fun onStart() {
super.onStart()
initiateSoundSampling()
//initiateFFT()
}
override fun onPause() {
soundSampler.audioRecord!!.stop()
soundSampler.audioRecord!!.release()
super.onPause()
}
private fun setupPermissions() {
val permission = checkSelfPermission(Manifest.permission.RECORD_AUDIO)
if (permission != PackageManager.PERMISSION_GRANTED) {
Log.i("tag", "Permission to record denied")
makeRequest()
}
}
private fun makeRequest() {
requestPermissions(arrayOf(Manifest.permission.RECORD_AUDIO),
RECORD_AUDIO_REQUEST_CODE)
}
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
RECORD_AUDIO_REQUEST_CODE -> {
if (grantResults.isEmpty() || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Log.i("tag", "Permission has been denied by user")
} else {
Log.i("tag", "Permission has been granted by user")
}
}
}
}
private fun initiateSoundSampling(){
try {
soundSampler = SoundReceiver(this)
} catch (e: Exception) {
Toast.makeText(applicationContext, "Cannot instantiate SoundReceiver", Toast.LENGTH_LONG).show()
}
try {
soundSampler.init()
} catch (e: Exception) {
Toast.makeText(applicationContext, "Cannot initialize SoundReceiver.", Toast.LENGTH_LONG).show()
}
}
private fun initiateSoundTransmitting() {
try {
soundTransmitter = SoundTransmitter(this)
} catch (e: Exception) {
Toast.makeText(applicationContext, "Cannot instantiate SoundTransmitter", Toast.LENGTH_LONG).show()
}
}
}