Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds dual samples for many sounds #286

Merged
merged 9 commits into from
Aug 20, 2020
Binary file modified app/src/main/assets/airplane_inflight.mp3
Binary file not shown.
Binary file modified app/src/main/assets/airplane_seatbelt_beeps.mp3
Binary file not shown.
Binary file removed app/src/main/assets/bonfire.mp3
Binary file not shown.
Binary file added app/src/main/assets/bonfire_0.mp3
Binary file not shown.
Binary file added app/src/main/assets/bonfire_1.mp3
Binary file not shown.
Binary file removed app/src/main/assets/coffee_shop.mp3
Binary file not shown.
Binary file added app/src/main/assets/coffee_shop_0.mp3
Binary file not shown.
Binary file added app/src/main/assets/coffee_shop_1.mp3
Binary file not shown.
Binary file removed app/src/main/assets/light_rain.mp3
Binary file not shown.
Binary file added app/src/main/assets/light_rain_0.mp3
Binary file not shown.
Binary file added app/src/main/assets/light_rain_1.mp3
Binary file not shown.
Binary file removed app/src/main/assets/morning_in_a_village.mp3
Binary file not shown.
Binary file added app/src/main/assets/morning_in_a_village_0.mp3
Binary file not shown.
Binary file added app/src/main/assets/morning_in_a_village_1.mp3
Binary file not shown.
Binary file removed app/src/main/assets/night.mp3
Binary file not shown.
Binary file added app/src/main/assets/night_0.mp3
Binary file not shown.
Binary file added app/src/main/assets/night_1.mp3
Binary file not shown.
Binary file removed app/src/main/assets/office.mp3
Binary file not shown.
Binary file added app/src/main/assets/office_0.mp3
Binary file not shown.
Binary file added app/src/main/assets/office_1.mp3
Binary file not shown.
Binary file removed app/src/main/assets/seaside.mp3
Binary file not shown.
Binary file added app/src/main/assets/seaside_0.mp3
Binary file not shown.
Binary file added app/src/main/assets/seaside_1.mp3
Binary file not shown.
Binary file removed app/src/main/assets/soft_wind.mp3
Binary file not shown.
Binary file added app/src/main/assets/soft_wind_0.mp3
Binary file not shown.
Binary file added app/src/main/assets/soft_wind_1.mp3
Binary file not shown.
Binary file removed app/src/main/assets/water_stream.mp3
Binary file not shown.
Binary file added app/src/main/assets/water_stream_0.mp3
Binary file not shown.
Binary file added app/src/main/assets/water_stream_1.mp3
Binary file not shown.
Binary file removed app/src/main/assets/wind_in_palm_trees.mp3
Binary file not shown.
Binary file added app/src/main/assets/wind_in_palm_trees_0.mp3
Binary file not shown.
Binary file added app/src/main/assets/wind_in_palm_trees_1.mp3
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,24 @@ class SoundLibraryFragment : Fragment(R.layout.fragment_sound_list) {
private val dataSet by lazy {
arrayListOf<SoundListItem>().also { list ->
var lastDisplayGroupResID = -1
val sounds = Sound.LIBRARY.values
.sortedWith(compareBy({ getString(it.displayGroupResID) }, { getString(it.titleResId) }))

for (sound in sounds) {
if (lastDisplayGroupResID != sound.displayGroupResID) {
lastDisplayGroupResID = sound.displayGroupResID
val sounds = Sound.LIBRARY.toSortedMap(
compareBy(
{ getString(Sound.get(it).displayGroupResID) },
{ getString(Sound.get(it).titleResId) }
)
)

sounds.forEach {
if (lastDisplayGroupResID != it.value.displayGroupResID) {
lastDisplayGroupResID = it.value.displayGroupResID
list.add(
SoundListItem(
R.layout.layout_list_item__sound_group_title, getString(lastDisplayGroupResID)
)
)
}

list.add(SoundListItem(R.layout.layout_list_item__sound, sound.key))
list.add(SoundListItem(R.layout.layout_list_item__sound, it.key))
}
}
}
Expand Down Expand Up @@ -158,14 +162,15 @@ class SoundLibraryFragment : Fragment(R.layout.fragment_sound_list) {
return
}

val sound = Sound.get(dataSet[position].data)
val isPlaying = players.containsKey(sound.key)
val soundKey = dataSet[position].data
val sound = Sound.get(soundKey)
val isPlaying = players.containsKey(soundKey)
holder.itemView.title.text = context.getString(sound.titleResId)
holder.itemView.seekbar_volume.isEnabled = isPlaying
holder.itemView.seekbar_time_period.isEnabled = isPlaying
holder.itemView.button_play.isChecked = isPlaying
if (isPlaying) {
requireNotNull(players[sound.key]).also {
requireNotNull(players[soundKey]).also {
holder.itemView.seekbar_volume.progress = it.volume
holder.itemView.seekbar_time_period.progress = it.timePeriod - Player.MIN_TIME_PERIOD
}
Expand Down
112 changes: 76 additions & 36 deletions app/src/main/java/com/github/ashutoshgngwr/noice/sound/Sound.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,98 +10,138 @@ import com.github.ashutoshgngwr.noice.R
* sound library supported by Noice.
*/
class Sound private constructor(
val key: String,
val src: Array<String>,
@StringRes val titleResId: Int,
@StringRes val displayGroupResID: Int,
val isLooping: Boolean
val isLooping: Boolean = true
) {

/**
* The simplified version of the constructor to initialize looping sounds.
*/
private constructor(
key: String,
@StringRes titleResId: Int,
@StringRes displayGroupResId: Int
) : this(key, titleResId, displayGroupResId, true)

companion object {
/**
* Static sound library with various effects. key is used to lookup the file in assets.
* Static sound library with various effects. All files in [src] array are looked up in the assets.
*/
val LIBRARY = mapOf(
"airplane_inflight" to Sound(
"airplane_inflight",
arrayOf("airplane_inflight.mp3"),
R.string.airplane_inflight,
R.string.sound_group__airplane
),
"airplane_seatbelt_beeps" to Sound(
"airplane_seatbelt_beeps",
arrayOf("airplane_seatbelt_beeps.mp3"),
R.string.airplane_seatbelt_beeps, R.string.sound_group__airplane,
false
),
"birds" to Sound("birds", R.string.birds, R.string.sound_group__jungle),
"bonfire" to Sound("bonfire", R.string.bonfire, R.string.sound_group__jungle),
"birds" to Sound(
arrayOf("birds.mp3"),
R.string.birds,
R.string.sound_group__jungle
),
"bonfire" to Sound(
arrayOf("bonfire_0.mp3", "bonfire_1.mp3"),
R.string.bonfire,
R.string.sound_group__jungle
),
"brownian_noise" to Sound(
"brownian_noise",
arrayOf("brownian_noise.mp3"),
R.string.brownian_noise,
R.string.sound_group__raw_noise
),
"coffee_shop" to Sound(
"coffee_shop",
arrayOf("coffee_shop_0.mp3", "coffee_shop_1.mp3"),
R.string.coffee_shop,
R.string.sound_group__public_gatherings
),
"distant_thunder" to Sound(
"distant_thunder",
arrayOf("distant_thunder.mp3"),
R.string.distant_thunder,
R.string.sound_group__monsoon,
false
),
"heavy_rain" to Sound("heavy_rain", R.string.heavy_rain, R.string.sound_group__monsoon),
"light_rain" to Sound("light_rain", R.string.light_rain, R.string.sound_group__monsoon),
"heavy_rain" to Sound(
arrayOf("heavy_rain.mp3"),
R.string.heavy_rain,
R.string.sound_group__monsoon
),
"light_rain" to Sound(
arrayOf("light_rain_0.mp3", "light_rain_1.mp3"),
R.string.light_rain,
R.string.sound_group__monsoon
),
"moderate_rain" to Sound(
"moderate_rain",
arrayOf("moderate_rain.mp3"),
R.string.moderate_rain,
R.string.sound_group__monsoon
),
"morning_in_a_village" to Sound(
"morning_in_a_village",
arrayOf("morning_in_a_village_0.mp3", "morning_in_a_village_1.mp3"),
R.string.morning_in_a_village,
R.string.sound_group__times_of_day
),
"moving_train" to Sound("moving_train", R.string.moving_train, R.string.sound_group__train),
"night" to Sound("night", R.string.night, R.string.sound_group__times_of_day),
"office" to Sound("office", R.string.office, R.string.sound_group__public_gatherings),
"pink_noise" to Sound("pink_noise", R.string.pink_noise, R.string.sound_group__raw_noise),
"moving_train" to Sound(
arrayOf("moving_train.mp3"),
R.string.moving_train,
R.string.sound_group__train
),
"night" to Sound(
arrayOf("night_0.mp3", "night_1.mp3"),
R.string.night,
R.string.sound_group__times_of_day
),
"office" to Sound(
arrayOf("office_0.mp3", "office_1.mp3"),
R.string.office,
R.string.sound_group__public_gatherings
),
"pink_noise" to Sound(
arrayOf("pink_noise.mp3"),
R.string.pink_noise,
R.string.sound_group__raw_noise
),
"rolling_thunder" to Sound(
"rolling_thunder",
arrayOf("rolling_thunder.mp3"),
R.string.rolling_thunder,
R.string.sound_group__monsoon,
false
),
"seaside" to Sound("seaside", R.string.seaside, R.string.sound_group__waterfront),
"soft_wind" to Sound("soft_wind", R.string.soft_wind, R.string.sound_group__wind),
"seaside" to Sound(
arrayOf("seaside_0.mp3", "seaside_1.mp3"),
R.string.seaside,
R.string.sound_group__waterfront
),
"soft_wind" to Sound(
arrayOf("soft_wind_0.mp3", "soft_wind_1.mp3"),
R.string.soft_wind,
R.string.sound_group__wind
),
"thunder_crack" to Sound(
"thunder_crack",
arrayOf("thunder_crack.mp3"),
R.string.thunder_crack,
R.string.sound_group__monsoon,
false
),
"train_horn" to Sound("train_horn", R.string.train_horn, R.string.sound_group__train, false),
"train_horn" to Sound(
arrayOf("train_horn.mp3"),
R.string.train_horn,
R.string.sound_group__train,
false
),
"water_stream" to Sound(
"water_stream",
arrayOf("water_stream_0.mp3", "water_stream_1.mp3"),
R.string.water_stream,
R.string.sound_group__waterfront
),
"white_noise" to Sound("white_noise", R.string.white_noise, R.string.sound_group__raw_noise),
"white_noise" to Sound(
arrayOf("white_noise.mp3"),
R.string.white_noise,
R.string.sound_group__raw_noise
),
"wind_chimes_of_shells" to Sound(
"wind_chimes_of_shells",
arrayOf("wind_chimes_of_shells.mp3"),
R.string.wind_in_chimes_of_shells,
R.string.sound_group__wind
),
"wind_in_palm_trees" to Sound(
"wind_in_palm_trees",
arrayOf("wind_in_palm_trees_0.mp3", "wind_in_palm_trees_1.mp3"),
R.string.wind_in_palm_trees,
R.string.sound_group__wind
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import kotlin.random.Random.Default.nextInt
* [Player] manages playback of a single [Sound]. It [PlaybackStrategy] instances to control media
* playback and keeps track of playback information such as [isPlaying], [volume] and [timePeriod].
*/
class Player(private val sound: Sound, playbackStrategyFactory: PlaybackStrategyFactory) {
class Player(val soundKey: String, playbackStrategyFactory: PlaybackStrategyFactory) {

companion object {
private val TAG = Player::class.simpleName
Expand All @@ -30,7 +30,7 @@ class Player(private val sound: Sound, playbackStrategyFactory: PlaybackStrategy

var timePeriod = DEFAULT_TIME_PERIOD

val soundKey = sound.key
private val sound = Sound.get(soundKey)

private var isPlaying = false
private var playbackStrategy = playbackStrategyFactory.newInstance(sound).also {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class PlayerManager(private val context: Context) :
*/
fun play(soundKey: String) {
if (!players.containsKey(soundKey)) {
players[soundKey] = Player(Sound.get(soundKey), playbackStrategyFactory)
players[soundKey] = Player(soundKey, playbackStrategyFactory)
}

if (playbackDelayed) {
Expand Down Expand Up @@ -340,7 +340,7 @@ class PlayerManager(private val context: Context) :
if (players.contains(it.soundKey)) {
player = requireNotNull(players[it.soundKey])
} else {
player = Player(Sound.get(it.soundKey), playbackStrategyFactory)
player = Player(it.soundKey, playbackStrategyFactory)
players[it.soundKey] = player
}

Expand Down