Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/turskyi/drummachine
A pure Kotlin project showcasing class usage, JUnit testing, and coroutine-based audio playback. Designed for JVM, not Android.
https://github.com/turskyi/drummachine
audio-playback concurrent-programming junit jvm kotlin kotlin-coroutines kotlin-example unit-testing
Last synced: 27 days ago
JSON representation
A pure Kotlin project showcasing class usage, JUnit testing, and coroutine-based audio playback. Designed for JVM, not Android.
- Host: GitHub
- URL: https://github.com/turskyi/drummachine
- Owner: Turskyi
- Created: 2020-10-31T22:18:41.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-07-26T02:52:15.000Z (5 months ago)
- Last Synced: 2024-07-26T03:50:06.215Z (5 months ago)
- Topics: audio-playback, concurrent-programming, junit, jvm, kotlin, kotlin-coroutines, kotlin-example, unit-testing
- Language: Kotlin
- Homepage: https://github.com/Turskyi/drummachine
- Size: 1.42 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner-direct-single.svg)](https://stand-with-ukraine.pp.ua)
# Pure Kotlin Project
This is a pure Kotlin project demonstrating the use of Kotlin features such as
classes, unit testing with JUnit, and coroutines for concurrent programming.
The project is not an Android project and runs on the JVM.## Project Structure
The project consists of three main components:
1. **Totaller Class**: A simple class that adds numbers to a total.
2. **TotallerTest Class**: A unit test class for the Totaller class using JUnit.
3. **Main** Function with Coroutines: Demonstrates the use of Kotlin
4. coroutines and plays audio files.## Totaller Class
This class maintains a running total of numbers added to it.
```kotlin
class Totaller(var total: Int = 0) {
fun add(num: Int): Int {
total += num
return total
}
}
```## TotallerTest Class
This class tests the functionality of the Totaller class.
```kotlin
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Testclass TotallerTest {
@Test
fun shouldBeAbleToAdd3And4() {
val totaller = Totaller()
Assertions.assertEquals(3, totaller.add(3))
Assertions.assertEquals(7, totaller.add(4))
Assertions.assertEquals(7, totaller.total)
}
}
```## Main Function with Coroutines
This demonstrates the use of Kotlin coroutines to play audio beats concurrently.
```kotlin
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.io.File
import javax.sound.sampled.AudioSystemsuspend fun playBeats(beats: String, file: String) {
val parts = beats.split("x")
var count = 0
for (part in parts) {
count += part.length + 1
if (part == "") {
playSound(file)
} else {
delay(100 * (part.length + 1L))
if (count < beats.length) {
playSound(file)
}
}
}
}fun playSound(file: String) {
val clip = AudioSystem.getClip()
val audioInputStream = AudioSystem.getAudioInputStream(File(file))
clip.open(audioInputStream)
clip.start()
}fun main() {
runBlocking {
launch {
playBeats("x-x-x-x-x-x", "audio/toms.aiff")
}
launch {
playBeats("x----x----x", "audio/crash_cymbal.aiff")
}
playBeats("--x--x---x-", "audio/high_hat.aiff")
}
}
```## How to Run the Project
### Prerequisites
- JDK 1.8 or higher
- Gradle## Running the Tests
To run the unit tests, use the following command:
```sh
./gradlew test
```## Notes
- Ensure you have the required audio files (toms.aiff, crash_cymbal.aiff,
high_hat.aiff) in the audio directory relative to the project root.
- This project uses kotlinx.coroutines for coroutine support and
javax.sound.sampled for audio playback.## Conclusion
This project showcases the simplicity and power of Kotlin for JVM applications.
It demonstrates unit testing with JUnit and concurrent programming with
coroutines, making it a great example for learning and reference.