Skip to content

Commit

Permalink
Add stop and share functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sribnyak committed Jun 2, 2020
1 parent 0329ffe commit e71a1ea
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.sribnyak.composer_plus"
minSdkVersion 17
targetSdkVersion 29
versionCode 9
versionName "1.1.4"
versionCode 10
versionName "1.2.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":9,"versionName":"1.1.4","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release","dirName":""},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":10,"versionName":"1.2.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release","dirName":""},"path":"app-release.apk","properties":{}}]
34 changes: 27 additions & 7 deletions app/src/main/java/com/sribnyak/composer_plus/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ package com.sribnyak.composer_plus

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.Html
import android.text.InputFilter
import android.text.InputType
import android.text.method.ScrollingMovementMethod
// import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.*
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.setPadding
import kotlin.math.*
import kotlin.math.max
import kotlin.math.min

const val MIN_V = 32
const val MAX_V = 1
Expand All @@ -29,6 +30,10 @@ val pitchClassStr: Array<String> = arrayOf(
"-"
)

lateinit var btnPlay: Button
fun btnPlayToPlay() { btnPlay.setText(R.string.btnPlay) }
fun btnPlayToStop() { btnPlay.setText(R.string.btnStop) }

class MainActivity : AppCompatActivity() {

private var currentOctave = 1
Expand Down Expand Up @@ -73,6 +78,7 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)

defaultPaddingSize = (defaultPaddingSize * resources.displayMetrics.density + 0.5).toInt()
btnPlay = findViewById(R.id.btnPlay)
editText = findViewById(R.id.editText)
val btnTempo: Button = findViewById(R.id.btnTempo)
btnTempo.text = getString(R.string.btnTempo, Sound.tempo)
Expand All @@ -95,7 +101,7 @@ class MainActivity : AppCompatActivity() {
} else {
editText.setText(note.toString())
}
if (!Player.musicPlaying)
if (Player.state != Player.MUSIC_PLAYING)
Player.play(note)
} else {
showToast(getString(R.string.maxLen))
Expand Down Expand Up @@ -148,13 +154,17 @@ class MainActivity : AppCompatActivity() {
note.dot = !note.dot
editText.setText(editText.text.toString().dropLastWhile { it != ' ' } + note)
melody.add(note)
if (!Player.musicPlaying)
if (Player.state != Player.MUSIC_PLAYING)
Player.play(note)
}
}
findViewById<Button>(R.id.btnPlay).setOnClickListener {
if (!Player.musicPlaying)
Player.play(melody) // TODO: make stoppable
btnPlay.setOnClickListener {
if (Player.state == Player.NOT_PLAYING)
Player.play(melody)
else if (Player.state == Player.MUSIC_PLAYING)
Player.stop()
else
showToast(getString(R.string.waitToPlay))
}
btnTempo.setOnClickListener {
val minValue = 30
Expand Down Expand Up @@ -255,6 +265,16 @@ class MainActivity : AppCompatActivity() {
}
return true
}
R.id.menuShareMelody -> {
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, SaveAndShare.makeText(editText.text.toString()))
type = "text/plain"
}
val shareIntent = Intent.createChooser(sendIntent, null)
startActivity(shareIntent)
return true
}
R.id.menuHelp -> {
val builder = AlertDialog.Builder(this)
val txt = defaultTextView(this)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/sribnyak/composer_plus/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.sribnyak.composer_plus
import java.util.*

object Parser {
private const val SUCCESS = 0
const val SUCCESS = 0
const val TOO_LONG = 1
const val SYNTAX_ERROR = 2

Expand Down
24 changes: 21 additions & 3 deletions app/src/main/java/com/sribnyak/composer_plus/Player.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import android.os.AsyncTask

object Player {
const val RATE = 22050
var musicPlaying = false

const val NOT_PLAYING = 0
const val NOTE_PLAYING = 1
const val MUSIC_PLAYING = 2

var state = 0
var calls = 0

@Suppress("DEPRECATION")
val audioTrack = AudioTrack(
Expand All @@ -32,21 +38,33 @@ object Player {
override fun doInBackground(vararg sound: ShortArray): Int {
audioTrack.write(sound[0], 0, sound[0].size)
audioTrack.play()
musicPlaying = false
calls--
if (calls == 0) {
state = NOT_PLAYING
btnPlayToPlay()
}
return 0
}
}

private fun play(notes: Array<Note>) {
calls++
PlaySoundTask().execute(Sound.toSound(notes))
}

fun play(melody: Melody) {
musicPlaying = true
state = MUSIC_PLAYING
btnPlayToStop()
play(melody.toArray())
}

fun play(note: Note) {
state = NOTE_PLAYING
play(arrayOf(note))
}

fun stop() {
audioTrack.pause()
audioTrack.flush()
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/sribnyak/composer_plus/SaveAndShare.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sribnyak.composer_plus

object SaveAndShare {
private const val BC = '@'
fun makeText(body: String): String {
return "$BC Composer Plus v${BuildConfig.VERSION_NAME}\n$BC tempo=${Sound.tempo}\n$body"
}
}
1 change: 0 additions & 1 deletion app/src/main/java/com/sribnyak/composer_plus/Sound.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,4 @@ object Sound {
}
return sound
}

}
8 changes: 6 additions & 2 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
android:orderInCategory="101"
android:title="@string/menuTranspose" />
<item
android:id="@+id/menuHelp"
android:id="@+id/menuShareMelody"
android:orderInCategory="102"
android:title="@string/menuShareMelody" />
<item
android:id="@+id/menuHelp"
android:orderInCategory="103"
android:title="@string/menuHelp"
app:showAsAction="never" />
<item
android:id="@+id/menuAbout"
android:orderInCategory="103"
android:orderInCategory="104"
android:title="@string/menuAbout"
app:showAsAction="never" />
</menu>
6 changes: 3 additions & 3 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
<string name="cancel">Отмена</string>
<string name="apply">Применить</string>
<string name="maxLen">Это максимальная длина, хватит</string>
<string name="waitToPlay">Вы сможете воспроизвести мелодию только после воспроизведения введённых нот.</string>
<string name="menuTextEditing">Текстовое редактирование</string>
<string name="menuShareMelody">Поделиться мелодией</string>
<string name="menuHelp">Помощь</string>
<string name="menuAbout">О приложении</string>
<string name="textEditApplied">Изменения сохранены!</string>
Expand Down Expand Up @@ -60,9 +62,7 @@
\nНота — &lt;длительность>[.][#]{c|d|e|f|g|a|b}&lt;октава>
\nПауза — &lt;длительность>[.]-
\n
\nРежим текстового редактирования автоматически заменяет прописные буквы на строчные, а символы табуляции, переноса строки, обычные и неразрывные пробелы и последовательности таких символов — на пробелы.
\n
\nВоспроизведение мелодии начинается после воспроизведения введённых нот.</string>
\nРежим текстового редактирования автоматически заменяет прописные буквы на строчные, а символы табуляции, переноса строки, обычные и неразрывные пробелы и последовательности таких символов — на пробелы.</string>
<string name="textAbout">&lt;b>%s&lt;/b> v%s
&lt;br>
&lt;br>Приложение создано в качестве замены синтезатора мелодий Nokia. Вы можете создавать здесь простые одноголосные мелодии с помощью текстового ввода или клавиатуры фортепиано.
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
<string name="cancel">Cancel</string>
<string name="apply">Apply</string>
<string name="maxLen">This is the maximum length, stop please</string>
<string name="waitToPlay">You can play the melody only after the entered notes are played</string>
<string name="menuTextEditing">Text editing</string>
<string name="menuShareMelody">Share the melody</string>
<string name="menuHelp">Help</string>
<string name="menuAbout">About</string>
<string name="textEditApplied">Changes applied!</string>
Expand Down Expand Up @@ -59,9 +61,7 @@
\nNote — &lt;value>[.][#]{c|d|e|f|g|a|b}&lt;octave>
\nPause — &lt;value>[.]-
\n
\nText editing mode automatically replaces uppercase letters with lowercase letters. Tabs, line breaks, regular and non-breaking spaces, and sequences of such characters are replaced with spaces.
\n
\nMelody playing starts after the entered notes are played.</string>
\nText editing mode automatically replaces uppercase letters with lowercase letters. Tabs, line breaks, regular and non-breaking spaces, and sequences of such characters are replaced with spaces.</string>
<string name="textAbout">&lt;b>%s&lt;/b> v%s
&lt;br>
&lt;br>This app was created as a replacement for the Nokia composer app. You can create here simple single-voice melodies using text input or the piano keyboard.
Expand Down

0 comments on commit e71a1ea

Please sign in to comment.