Skip to content

Commit

Permalink
add audio bars
Browse files Browse the repository at this point in the history
  • Loading branch information
zoff99 committed Oct 31, 2023
1 parent 0f90598 commit 23ee71c
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 1 deletion.
116 changes: 116 additions & 0 deletions src/main/java/com/zoffcc/applications/trifa/AudioBar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* [TRIfA], Java part of Tox Reference Implementation for Android
* Copyright (C) 2020 Zoff <[email protected]>
* <p>
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

package com.zoffcc.applications.trifa;

import java.awt.*;
import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JPanel;

public class AudioBar extends JPanel
{
private static final String TAG = "trifa.AudioBar";

public static AudioBar audio_in_bar = new AudioBar();
public static AudioBar audio_out_bar = new AudioBar();

private int cur_value = 0;
private final int yellow_value = 75;
private final int red_value = 85;

AudioBar()
{
super();
setDoubleBuffered(true);
setPreferredSize(new Dimension(300, 4));
setSize(300, 4);
revalidate();
repaint();
set_cur_value(0, this);
}

@Override
public void paint(Graphics g)
{
super.paint(g);
setBackground(Color.black);
int w = this.getWidth();
int h = this.getHeight();
if (cur_value > red_value)
{
g.setColor(Color.RED);
g.fillRect(0, 0, (w / 100) * cur_value, h);
g.setColor(Color.YELLOW);
g.fillRect(0, 0, (w / 100) * red_value, h);
g.setColor(Color.GREEN);
g.fillRect(0, 0, (w / 100) * yellow_value, h);
}
else if (cur_value > yellow_value)
{
g.setColor(Color.YELLOW);
g.fillRect(0, 0, (w / 100) * cur_value, h);
g.setColor(Color.GREEN);
g.fillRect(0, 0, (w / 100) * yellow_value, h);
}
else if (cur_value == 0)
{
// nothing to do, already full black bg
}
else
{
g.setColor(Color.GREEN);
g.fillRect(0, 0, (w / 100) * cur_value, h);
}
}

public static void set_cur_value(int value, AudioBar c)
{
if (c != null)
{
c.cur_value = value;
Log.i(TAG, "set_cur_value:*********AApaint*********");
c.repaint();
}
else
{
Log.i(TAG, "set_cur_value:EE01");
}
}

public static float audio_vu(byte[] data, int sample_count)
{
float sum = 0.0f;
final float factor = (100.0f / 140.0f);

if (sample_count > 1)
{
for (int i = 0; i < sample_count; i = i + 2)
{
short s = (short) ((data[i] & 0xff) | (data[i + 1] << 8));
sum = sum + (Math.abs(s) / 32767.0f);
}

float vu = (float) (20.0f * Math.log(sum));
return vu * factor;
}
return 0.0f;
}
}
21 changes: 21 additions & 0 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import androidx.compose.ui.window.WindowPosition
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import com.zoffcc.applications.ffmpegav.AVActivity
import com.zoffcc.applications.trifa.AudioBar
import com.zoffcc.applications.trifa.HelperGeneric.PubkeyShort
import com.zoffcc.applications.trifa.JPictureBox
import com.zoffcc.applications.trifa.JPictureBoxOut
Expand Down Expand Up @@ -102,6 +103,7 @@ import org.briarproject.briar.desktop.ui.UiPlaceholder
import org.briarproject.briar.desktop.ui.VerticalDivider
import org.briarproject.briar.desktop.utils.InternationalizationUtils.i18n
import java.awt.Component
import java.awt.LayoutManager
import java.awt.Toolkit
import java.util.*
import java.util.concurrent.Executors
Expand Down Expand Up @@ -575,6 +577,25 @@ fun App()
}
}
}
SwingPanel(
modifier = Modifier.size(250.dp,5.dp),
factory = {
JPanel(SingleComponentAspectRatioKeeperLayout(), true).apply {
add(AudioBar.audio_out_bar)
}
},
update = { }
)
Spacer(modifier = Modifier.height(5.dp))
SwingPanel(
modifier = Modifier.size(250.dp,5.dp),
factory = {
JPanel(SingleComponentAspectRatioKeeperLayout(), true).apply {
add(AudioBar.audio_in_bar)
}
},
update = { }
)
UIScaleItem(
label = i18n("UI Scale"),
description = "${i18n("current_value:")}: "
Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/com/zoffcc/applications/trifa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.zoffcc.applications.sorm.FileDB
import com.zoffcc.applications.sorm.Filetransfer
import com.zoffcc.applications.sorm.GroupMessage
import com.zoffcc.applications.sorm.Message
import com.zoffcc.applications.trifa.AudioBar.audio_out_bar
import com.zoffcc.applications.trifa.AudioSelectOutBox.semaphore_audio_out_convert
import com.zoffcc.applications.trifa.AudioSelectOutBox.semaphore_audio_out_convert_active_threads
import com.zoffcc.applications.trifa.AudioSelectOutBox.semaphore_audio_out_convert_max_active_threads
Expand Down Expand Up @@ -116,6 +117,7 @@ class MainActivity
@JvmField public var PREF__auto_accept_image = true
@JvmField var PREF__auto_accept_video = true
@JvmField var PREF__auto_accept_all_upto = true
const val AUDIO_VU_MIN_VALUE = -20f
//
var video_buffer_1: ByteBuffer? = null
var buffer_size_in_bytes = 0
Expand Down Expand Up @@ -1106,6 +1108,20 @@ class MainActivity
{
semaphore_audio_out_convert.release()
}
var global_audio_out_vu: Float = AUDIO_VU_MIN_VALUE
if (sample_count > 0)
{
val vu_value = AudioBar.audio_vu(audio_out_byte_buffer, sample_count.toInt())
global_audio_out_vu = if (vu_value > AUDIO_VU_MIN_VALUE)
{
vu_value
} else
{
0f
}
}
val global_audio_out_vu_ = global_audio_out_vu
AudioBar.set_cur_value(global_audio_out_vu_.toInt(), audio_out_bar)
}
t_audio_pcm_play.start()
}
Expand Down
28 changes: 27 additions & 1 deletion src/main/kotlin/com/zoffcc/applications/trifa2/ChatApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.zoffcc.applications.ffmpegav.AVActivity
import com.zoffcc.applications.ffmpegav.AVActivity.ffmpegav_ByteBufferCompat
import com.zoffcc.applications.ffmpegav.AVActivity.ffmpegav_close_audio_in_device
import com.zoffcc.applications.ffmpegav.AVActivity.ffmpegav_close_video_in_device
import com.zoffcc.applications.ffmpegav.AVActivity.ffmpegav_init
Expand All @@ -41,9 +40,13 @@ import com.zoffcc.applications.ffmpegav.AVActivity.ffmpegav_set_video_capture_ca
import com.zoffcc.applications.ffmpegav.AVActivity.ffmpegav_stop_audio_in_capture
import com.zoffcc.applications.ffmpegav.AVActivity.ffmpegav_stop_video_in_capture
import com.zoffcc.applications.trifa.AVState
import com.zoffcc.applications.trifa.AudioBar
import com.zoffcc.applications.trifa.AudioBar.audio_in_bar
import com.zoffcc.applications.trifa.ByteBufferCompat
import com.zoffcc.applications.trifa.HelperGroup.tox_group_by_groupid__wrapper
import com.zoffcc.applications.trifa.Log
import com.zoffcc.applications.trifa.MainActivity
import com.zoffcc.applications.trifa.MainActivity.Companion.AUDIO_VU_MIN_VALUE
import com.zoffcc.applications.trifa.MainActivity.Companion.on_call_ended_actions
import com.zoffcc.applications.trifa.MainActivity.Companion.sent_message_to_db
import com.zoffcc.applications.trifa.MainActivity.Companion.set_JNI_audio_buffer
Expand Down Expand Up @@ -242,6 +245,29 @@ fun start_outgoing_video(friendpubkey: String)
channels = out_channels,
sampling_rate = out_sample_rate.toLong())
// Log.i(TAG, "tox_audio_res=" + tox_audio_res)
val sample_count_: Int = out_samples
val t_audio_bar_set: Thread = object : Thread()
{
override fun run()
{
var global_audio_in_vu: Float = AUDIO_VU_MIN_VALUE
if (sample_count_ > 0)
{
audio_buffer_1.rewind()
val data_compat = ByteBufferCompat(audio_buffer_1)
val vu_value: Float = AudioBar.audio_vu(data_compat.array(), sample_count_)
global_audio_in_vu = if (vu_value > AUDIO_VU_MIN_VALUE)
{
vu_value
} else
{
0f
}
}
AudioBar.set_cur_value(global_audio_in_vu.toInt(), audio_in_bar)
}
}
t_audio_bar_set.start()

/* DEBUG ONLY ----------------------------
try
Expand Down

0 comments on commit 23ee71c

Please sign in to comment.