Sound library no longer works?

Hi everyone,
I’m looking for a soundlibrary to use in Android mode.
I use usually Minim library in Java mode but Minim don’t work in Android mode.
So I would like to use the Sound library because It looks like Sound works with android.
To begin with Sound I first tried examples provided with the Sound library like “AudioWaveform” and others in the Java mode but without success.
Here is the error message when I try to run the sketch.

oct. 29, 2020 1:19:59 PM com.jsyn.devices.javasound.JavaSoundAudioDevice <init>
INFOS: JSyn: default output latency set to 80 msec for Windows 7
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.
	at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:513)
	at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:121)
	at com.jsyn.devices.javasound.JavaSoundAudioDevice$JavaSoundInputStream.start(Unknown Source)
	at com.jsyn.engine.SynthesisEngine$EngineThread.run(Unknown Source)
java.lang.NullPointerException
	at com.jsyn.devices.javasound.JavaSoundAudioDevice$JavaSoundInputStream.read(Unknown Source)
	at com.jsyn.devices.javasound.JavaSoundAudioDevice$JavaSoundInputStream.read(Unknown Source)
	at com.jsyn.engine.SynthesisEngine$EngineThread.run(Unknown Source)
java.lang.RuntimeException: AudioInput stop attempted when no line created.
	at com.jsyn.devices.javasound.JavaSoundAudioDevice$JavaSoundInputStream.stop(Unknown Source)
	at com.jsyn.engine.SynthesisEngine$EngineThread.run(Unknown Source)

And this only with sketches written with the Sound library!
Sketches written with the Minim or Bead libs works fine.
I have tried with Java7 : it’s the same.
I don’t understand what append.
I’am on Windows7pro, Processing 3.5.4 and Java8.
Can anyone help me?

Thanks in advance for your replies.

Hi @Electronix
I usually use the cassete library especially made for Android.
But you can also add a few lines of native code to play a sound file in the data folder.
More codes here.

import android.media.MediaPlayer;
import android.content.res.Resources;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.content.Context;
import android.app.Activity;
 
MediaPlayer player = new MediaPlayer();
AssetFileDescriptor fd;
Context context;
Activity activity;
String sound_file_name = "Over_the_horizon.mp3";

void setup() {
  fullScreen();
  background(0, 0, 200);
  orientation(PORTRAIT);
  textSize(60);
  textAlign(CENTER, CENTER);
  activity = this.getActivity();
  context = activity.getApplicationContext();
  try {
    fd = context.getAssets().openFd(sound_file_name);
    player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
  }
  catch (IOException e) {
    e.printStackTrace();
  }
  text("Press to start", width/2, height/2);
}

void draw() {
}

void mousePressed() {
  try {
    player.prepare();
    player.start();
  }
  catch (Exception e) {
    e.printStackTrace();
  }
}

void onPause() {
  super.onPause();
  if (player!=null) {
    player.release();
    player= null;
  }
}

void onStop() {
  super.onStop();
  if (player!=null) {
    player.release();
    player= null;
  }
}

void onDestroy() {
  super.onDestroy();
  if (player!=null) {
    player.release();
    player= null;
  }
}
1 Like

Hi @noel
Thanks for your reply.
Cassette library is for playing a sound.
I look for a library to make sounds with functions to control oscillators, envelopes,etc. Therefore cassette lib is not suitable for this.

I never saw a working example of these working on Android. Even playing a simple sound file with this library on Android has many issues on the forums. I’ve spent quite some hours now, to get the FFT and waveform data byte array with the Visualizer class, but I still battle with a bunch of errors. I also had a look into frequency generation, which is easier. As soon as I get it working I’ll post it here.

1 Like

@noel
Ok, thanks.
But one thing at a time, for the moment I would just like to run the Sound lib in JAVA mode.

I got confused now…

@noel
My starting post was not very clear, my apologies.
Until now I was only working in Java mode, now I start working in Android mode.
So far, I have not yet tried code with lib Sound in Android mode. I try first in Java mode because I have never worked with the Sound lib before, only with the Minim lib. The Minim library is better for synthesizing sounds indeed but don’t work on Android.

Can’t anyone help me to find a solution to this problem?

Does it not work at all?

No, see details on my first post at the top.
To begin, I first tried in the Java mode but without success.

Im by no means super knowledgeable about processing but as i would also like to work with android and I will take a look into this as soon as i can. It would be nice for all the pc code to work in android mode, as i think processing could be a great platform for android development, but there are currently a lot of hurdles. Anyways ill post back soon.

In principle it should be possible because the lib on Github sound writes:
(But I still don’t have it working.)

How to build

  1. git clone git@github.com:processing/processing-sound.git
  2. (optional: copy (or soft-link) processing-core.zip from your local Processing for Android mode as well as your Android SDK’s android.jar , API level 26 or higher, into the library/ folder. If you don’t do this, these will be downloaded from GitHub instead. Note that as of version 2.2 the sound library is compiled against Processing’s Android mode rather than the normal Processing core.jar in order to more smoothly support AudioIn on Android. Other dependencies, in particular Phil Burk’s JSyn engine on which this library is based, are also all downloaded automatically by ant.)
  3. ant dist (or, alternatively, run build.xml from within Eclipse)
    The resulting sound.zip can be extracted into your Processing installation’s libraries/ >folder.

In the meantime, I’m trying to understand how sound frequency is generated. on Android.
So far I have this code working on my Android 10 device, but strangely it doesn’t work on my Lollipop. Maybe you can help me by running this code on your device, and tell me if there are problems and if you like my song.

import android.media.AudioTrack; 
import android.media.AudioManager; 
import android.media.AudioFormat; 
import java.lang.SuppressWarnings; 

// Index of notes in in note_frequencies array 
int Gl = 0, A = 1, B = 2, C = 3, D = 4, E = 5, G = 6, Ah = 7, Bh = 8, Ch = 9; 
//                         Gl,  A,   B,   C,   D,   E,   G,   Ah,  Bh,  Ch 
int[] note_frequencies = {392, 440, 494, 523, 587, 659, 784, 880, 988, 1046}; 
int[] song_notes = {A, A, E, D, C, B, B, B, D, C, B, A, A, Ch, Bh, Ch, Bh, Ch, A, A, C, B, C, B, C}; // The song 
int[] final_notes = {C, C, C, C, E, E, E, E, D, D, D, D, G, G, G, G, Ah, Ah, Ah, Ah, Ah, Ah, Ah, D, C, B, Gl, A, A, A}; 
// index of long notes in notes array 
int[] note = {1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0};

int sample_rate = 44100; 

AudioTrack[] audio_track = new AudioTrack[note_frequencies.length]; 
void setup() { 
  background(0, 0, 200); 
  fill(255); 
  textAlign(CENTER, CENTER); 
  String str = "Click to start song."; 
  textSize(12); 
  float twf = width/textWidth(str);
  ; 
  textSize(9*twf); 
  text(str, width/2, height/2); 
  int note_duration = sample_rate/5; 
  byte long_note[][] = new byte[note_frequencies.length][note_duration]; 
  for (int ln = 0; ln < note_frequencies.length; ln++) { 
    for (int i = 0; i < long_note[ln].length; i++) { 
      byte sample = (byte)(sin(TWO_PI*note_frequencies[ln]*i/sample_rate)*255); 
      long_note[ln][i] = sample;
    }
  } //@SuppressWarnings("deprecation") 
  for (int ln = 0; ln < note_frequencies.length; ln++) { 
    audio_track[ln] = new AudioTrack( AudioManager.STREAM_MUSIC, sample_rate, AudioFormat.CHANNEL_OUT_DEFAULT, AudioFormat.ENCODING_PCM_8BIT, long_note[ln].length, AudioTrack.MODE_STATIC ); 
    audio_track[ln].write(long_note[ln], 0, long_note[0].length);
  }
} 

void draw() {
} 

void mousePressed() { 
  for ( int i = 0; i < 2; i++) { 
    for (int sn = 0; sn < song_notes.length; sn++) { 
      audio_track[song_notes[sn]].play(); 
      int start_time = millis(); 
      int pause_between_notes; 
      if (note[sn] == 1) pause_between_notes = 350; 
      else pause_between_notes = 150; 
      while (millis()-start_time < pause_between_notes) {
      }; 
      if (i == 1 && sn == 18) break; 
      audio_track[song_notes[sn]].stop();
    }
  } 
  for (int fn = 0; fn < final_notes.length; fn++) { 
    int start_time = millis(); 
    int pause_between_notes = 200; 
    audio_track[final_notes[fn]].play(); 
    while (millis()-start_time < pause_between_notes) {
    }; 
    audio_track[final_notes[fn]].stop();
  }
} 

void onStop () { 
  for (int i = 0; i < audio_track.length; i++) { 
    audio_track[i].release(); 
    audio_track[i] = null;
  }
} 

void onPause () { 
  for (int i = 0; i < audio_track.length; i++) { 
    audio_track[i].release(); 
    audio_track[i] = null;
  }
}

Runs ok on Samsung S2 tablet; Android version 6.0.1. Does crash after song has ended. Don’t recognize the tune.

The same happens on my Lollipop.
You didn’t recognize the song because it doesn’t play all the notes. It’s a popular song.

Runs on huawei p30 running android 10 no crashes through apde. Can’t say i recognise the song either…

Is it similar to this tune? https://drive.google.com/file/d/1QqBZc5abkbmc_8yldhAnvTIZEdBFbx9M/view?usp=sharing

Yup thats it! So i guess sound works on android.

The Android library wizard is @uheinema. Maybe he is willing to figure out, how to build this library.

I have been using Processing’s sound library for a while, you can download the pre-built sound.zip from the GitHub releases page. (The same library zip works for both desktop and Android. Tested with APDE.) Even if you have issues using it on desktop (which might warrant filing a bug report) it should still work on Android.

1 Like

Hi @calsign great to see you here again!
I only use APDE, and have tried several releases from the app and the library as well.
Although the app complains that sound-dex.jar does not exist, the examples are build (App mode) but when installed they all crash without warnings. I’ve also tried to build the library as mentioned on Github and included the dex.jar, but without success on both my phones Lollipop and Android 10. Could you zip your working library folder and post it.
Thanks.