Skip to content

Commit

Permalink
+ added custom dBm handler
Browse files Browse the repository at this point in the history
* implemented speech recognizer dBm handler
* updated rendering logic
* renamed resources and attributes
* updated readme
  • Loading branch information
Iojjj committed Mar 4, 2016
1 parent 306e84b commit 3c91cbe
Show file tree
Hide file tree
Showing 27 changed files with 847 additions and 206 deletions.
37 changes: 26 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ To include this library to your project add dependency in **build.gradle** file:

```groovy
dependencies {
compile 'com.cleveroad:audiovisualization:0.9.0'
compile 'com.cleveroad:audiovisualization:0.9.1'
}
```

Expand Down Expand Up @@ -47,13 +47,13 @@ Via XML:
android:id="@+id/visualizer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:bubble_size="@dimen/bubble_size"
app:randomize_bubble_size="true"
app:wave_height="@dimen/wave_height"
app:footer_height="@dimen/footer_height"
app:waves_count="7"
app:layers_count="4"
app:background_color="@color/color_bg"
app:av_bubble_size="@dimen/bubble_size"
app:av_randomize_bubble_size="true"
app:av_wave_height="@dimen/wave_height"
app:av_footer_height="@dimen/footer_height"
app:av_waves_count="7"
app:av_layers_count="4"
app:av_background_color="@color/color_bg"
/>
```

Expand Down Expand Up @@ -90,18 +90,21 @@ Via Java code:
audioVisualization = (AudioVisualization) glAudioVisualizationView;
}

...

...
```

To connect audio visualization view to audio output you can use **linkTo(int)** or **linkTo(MediaPlayer)** methods.
To connect audio visualization view to audio output you can use **linkTo(int)**, **linkTo(MediaPlayer)** or **linkTo(DbmHandler)** methods.

```JAVA
// connecting to device's output mix
audioVisualization.linkTo(0);
...
MediaPlayer mp = MediaPlayer.create(...);
audioVisualization.linkTo(mp);
...
SpeechRecognizerDbmHandler handler = new SpeechRecognizerDbmHandler(context);
handler.innerRecognitionListener(...);
audioVisualization.linkTo(handler);
```

You must always call **onPause** method to pause visualization and stop wasting CPU resources for computations in vain. As soon as your view appears in sight of user, call **onResume**.
Expand Down Expand Up @@ -130,6 +133,18 @@ When user leaves screen with audio visualization view, don't forget to free reso
}
```

<br />
## Changelog

| Version | Changes |
| --- | --- |
| v.0.9.1 | Added ability to set custom dBm handler implementations; implemented SpeechRecognizerDbmHandler |
| v.0.9.0 | First public release |

#### Migrations from v.0.9.0 to v.0.9.1
* All library resources and attributes were renamed, prefix `av_` was added. If you're adding Audio Visualization view through XML, make sure you'd properly renamed all attributes. See updated example above.
* All library resources were marked as private. If you're pointing to any library resource (color, dimen, etc), Android Studio will warn you.

<br />
#### Support ####
* * *
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:label="@string/av_app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.view.ViewGroup;

import com.cleveroad.audiovisualization.AudioVisualization;
import com.cleveroad.audiovisualization.DbmHandler;
import com.cleveroad.audiovisualization.GLAudioVisualizationView;

/**
Expand All @@ -31,17 +32,16 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
.setFooterHeight(R.dimen.footer_height)
.setWavesCount(7)
.setLayersCount(4)
.setBackgroundColorRes(R.color.color_bg)
.setLayerColors(R.array.colors)
.setBackgroundColorRes(R.color.av_color_bg)
.setLayerColors(R.array.av_colors)
.build();
// return inflater.inflate(R.layout.fragment_gles, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
audioVisualization = (AudioVisualization) view;
audioVisualization.linkTo(0);
audioVisualization.linkTo(DbmHandler.newVisualizerHandler(0));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/cleveroad/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected void onResume() {

private void openFragment() {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, AudioVisualizationFragment.newInstance())
.add(R.id.container, MainFragment.newInstance())
.commit();
}
}
40 changes: 40 additions & 0 deletions app/src/main/java/com/cleveroad/example/MainFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.cleveroad.example;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

/**
* Main fragment.
*/
public class MainFragment extends ListFragment implements AdapterView.OnItemClickListener {

public static MainFragment newInstance() {
return new MainFragment();
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(), R.array.selection_items, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.container, AudioVisualizationFragment.newInstance())
.addToBackStack(null)
.commit();
} else if (position == 1) {
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.container, SpeechRecognitionFragment.newInstance())
.addToBackStack(null)
.commit();
}
}
}
147 changes: 147 additions & 0 deletions app/src/main/java/com/cleveroad/example/SpeechRecognitionFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.cleveroad.example;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.cleveroad.audiovisualization.AudioVisualization;
import com.cleveroad.audiovisualization.DbmHandler;
import com.cleveroad.audiovisualization.SpeechRecognizerDbmHandler;

/**
* Fragment with audio visualization view.
*/
public class SpeechRecognitionFragment extends Fragment {

public static SpeechRecognitionFragment newInstance() {
return new SpeechRecognitionFragment();
}

private AudioVisualization audioVisualization;
private Button btnRecognize;
private SpeechRecognizerDbmHandler handler;
private boolean recognizing;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gles, container, false);
audioVisualization = (AudioVisualization) view.findViewById(R.id.visualizer_view);
btnRecognize = (Button) view.findViewById(R.id.btn_recognize);
return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
btnRecognize.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (recognizing) {
handler.stopListening();
} else {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
handler.startListening(intent);
}
btnRecognize.setEnabled(false);
}
});
handler = DbmHandler.newSpeechRecognizerHandler(getContext());
handler.innerRecognitionListener(new SimpleRecognitionListener() {

@Override
public void onReadyForSpeech(Bundle params) {
super.onReadyForSpeech(params);
onStartRecognizing();
}

@Override
public void onResults(Bundle results) {
super.onResults(results);
onStopRecognizing();
}

@Override
public void onError(int error) {
super.onError(error);
onStopRecognizing();

}
});
audioVisualization.linkTo(handler);
}

private void onStopRecognizing() {
recognizing = false;
btnRecognize.setText(R.string.start_recognition);
btnRecognize.setEnabled(true);
}

private void onStartRecognizing() {
btnRecognize.setText(R.string.stop_recognition);
btnRecognize.setEnabled(true);
recognizing = true;
}

@Override
public void onDestroyView() {
audioVisualization.release();
super.onDestroyView();
}

private static class SimpleRecognitionListener implements RecognitionListener {

@Override
public void onReadyForSpeech(Bundle params) {

}

@Override
public void onBeginningOfSpeech() {

}

@Override
public void onRmsChanged(float rmsdB) {

}

@Override
public void onBufferReceived(byte[] buffer) {

}

@Override
public void onEndOfSpeech() {

}

@Override
public void onError(int error) {

}

@Override
public void onResults(Bundle results) {

}

@Override
public void onPartialResults(Bundle partialResults) {

}

@Override
public void onEvent(int eventType, Bundle params) {

}
}
}
34 changes: 24 additions & 10 deletions app/src/main/res/layout/fragment_gles.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<com.cleveroad.audiovisualization.GLAudioVisualizationView
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/visualizer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:bubble_size="@dimen/bubble_size"
app:randomize_bubble_size="true"
app:wave_height="@dimen/wave_height"
app:footer_height="@dimen/footer_height"
app:waves_count="7"
app:layers_count="4"
app:background_color="@color/color_bg"
/>
>
<com.cleveroad.audiovisualization.GLAudioVisualizationView
android:id="@+id/visualizer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:av_bubble_size="@dimen/bubble_size"
app:av_randomize_bubble_size="true"
app:av_wave_height="@dimen/wave_height"
app:av_footer_height="@dimen/footer_height"
app:av_waves_count="7"
app:av_layers_count="4"
app:av_background_color="@color/av_color_bg"
/>

<Button
android:id="@+id/btn_recognize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.Button.Borderless"
android:text="@string/start_recognition"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
7 changes: 7 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="selection_items">
<item>Visualizer</item>
<item>Speech Recognizer</item>
</string-array>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
<string name="btn_cancel">Cancel</string>
<string name="toast_permissions_not_granted">Permissions not granted.</string>
<string name="message_permissions"><![CDATA[To allow audio visualization view to process audio stream app needs an access to your <b>Microphone</b>.]]></string>
<string name="start_recognition">Start Recognition</string>
<string name="stop_recognition">Stop Recognition</string>
</resources>
1 change: 0 additions & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>

</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-beta5'
classpath 'com.android.tools.build:gradle:2.0.0-beta6'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Loading

0 comments on commit 3c91cbe

Please sign in to comment.