Pages

Monday, February 3, 2014

My adventures on playing and looping sounds on android, part 1

General wisdom

If you want to do simple looping, use SoundPool and MediaPlayer, they both have a function to do this.

If you want to do low-level manipulations of samples, .e.g. mucking around with byte arrays etc., adding effects, to create your own programmatic sound samples, use AudioTrack.

Use JetPlayer for midi tracks.

I'll provide some code about SoundPool and MediaPlayer to give a general idea.

AudioTrack and JetPlayer both deserve their own blog entry... TODO .

Soundpool

public class MainActivity extends Activity implements OnLoadCompleteListener {
    private final static int INVALID_STREAM_ID = -1;

//...

    private SoundPool soundPool;
    private int streamId = INVALID_STREAM_ID;
    private void setupSound() {
        final int SINGLE_STREAM = 1; // you can use multiple
        soundPool = new SoundPool(SINGLE_STREAM, AudioManager.STREAM_MUSIC, 0);
        soundPool.setOnLoadCompleteListener(this);
        // R.raw.click is located in res/raw/click.ogg
        streamId = soundPool.load(this, R.raw.click, 1); // load the sonic content
    }

//...

    private final static int SP_PLAY_ONCE = 0;
    private final static int SP_PLAY_LOOP = -1;
    /**
     *
     * This part below actually starts playing the sound,
     * otherwise you risk the chance of failing
     * to play because the system hasn't
     * finished loading yet
     */
    @Override
    public void onLoadComplete(SoundPool pool, int id, int status) {
        // to loop, see SP_PLAY_LOOP and replace SP_PLAY_ONCE
        pool.play(id, .5f, .5f, 1, SP_PLAY_ONCE, 1.0f);
    }

    @Override
    protected void onDestroy() {
        if (soundPool != null)
        {
            if (streamId != INVALID_STREAM_ID)
                soundPool.unload(streamId);
            soundPool.release();
        }
        super.onDestroy();
    }

You can also use the onLoadComplete callback to do specific stuff e.g. to start looping stuff under certain conditions, set a delay before playing the sonic content, etc.

MediaPlayer

public class MainActivity extends Activity implements onCompletionListener {

private MediaPlayer mediaPlayer;

// ...

   mediaPlayer = MediaPlayer.create(this, R.raw.click);
   // mediaPlayer.setLooping(true);
   mediaPlayer.start();
  // to reuse the mediaPlayer object, with a different sound
  // you have to #reset, #setDataSource #prepare

// ...

    @Override
    public void onCompletion(MediaPlayer mp) {
        // do stuff...
    }

    @Override
    protected void onDestroy() {
        if (mediaPlayer != null)
        {
            mediaPlayer.reset();
            mediaPlayer.release();
        }
        super.onDestroy();
    }

Getting the media file length in milliseconds

private int getSoundFileLengthInMs(int resId)
{
    MediaPlayer mp = MediaPlayer.create(this, resId);
    int duration = mp.getDuration();
    mp.release();
    return duration;
}

Stuff I learned from trial and error

  1. Do not combine Animation callbacks to do your timing with your sounds if you're doing precision work, in the order of milliseconds. Do not use those callbacks with anything important. Ever. I don't know why I have to relearn that lesson...
  2. Looping sounds using callbacks with either MediaPlayer's OnCompletionListener or some combination of SoundPool and Handler and Runnables, will not give you enough metronome like precision.
  3. Do not use TimerTask, to loop and set delays. The constant allocation of objects will cost you cpu-time and memory. Use a single Handler and single Runnable, to set delays.
    private MediaPlayer mediaPlayer; // initialized and prepared somewhere else
    
    //...    
    
        private Handler handler = new Handler();
        private Runnable runnable = new Runnable() {
            @Override
            public void run() {
               // do stuff beforehand...
               mediaPlayer.start();
            }
        };
    
    //...
    
        handler.postDelayed(this, delayInMs);
    
  4. AudioTrack is the only viable solution if you're planning on building a dynamic metronome application, due to the high degree of control.

The end!

Mass install on android devices via BASH

How to install on all your android devices consecutively

This is the command you copy paste on your bash prompt or script:

for d in $(adb devices | egrep "device$" | sed "s/[[:space:]]device$//");
do
  adb -s $d install -r bin/some.apk;
done

Determine that adb can be accessed from the $PATH variable. That or write out the full path where the adb command is located.

PATH="$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$PATH"

Happy coding!

P.S. broken-ass implementations of sed like on mac os x don't acknowledge the existence of

[[:space:]]

so, use something like

\s+

or

[\t ]+

and you might have to use sed like this:

sed -e ...

Friday, June 1, 2012

Yet another XMPP Client for Android

First of all, git it from github: https://github.com/Buggaboo/android_xmpp_client

The features

  • GreenDao, database access. Say goodbye to e.g. SQL injection exploits, crap manual data type conversions;
  • Smack library (xmpp), modified for Android, with bugs, but not crashworthy;
  • It vibrates messages to morse code; (3 june 2012: WIP)
  • The messages can be translatable to different languages, just by leveraging the xml resources;
  • Multiple provider support, i.e. connect with google, microsoft, meebo, whatever you want;
  • Licence: GPLv3;
  • Support for android api levels 10 thru 15 (from gingerbread to ice cream sandwich due to compatibility library, 80% of the android  market);
  • Support for android api level 15 (pure ice cream sandwich, less than 10%);
  • Support for fragments (12 june 2012: WIP);
  • Support for android Notifications;


 

 

  The general design

  • Sharing data between Contexts via the database: A context alpha (e.g. Activity or Service) pokes another context beta with an Intent with a Bundle payload containing the record id of a certain database table.
  • To prevent competition for the same database resource, certain services take randomized naps, aka Thread.sleep(Long);
  • A message entity has a buddy entity has a connection entity, i.e. I use foreign keys, to sort and group data etc., GreenDao generates all the code you need;
  • BroadcastReceivers and ArrayAdapters work together to refresh your ListViews.

Known Issues

  1.  There are no bounds on how much data is pulled from the database and rendered as a view, the ListViews are potential memory hogs;
  2. There are no arbitrary limits on data input, which could eat all your memory;
  3. I have not stress-tested how many connections can be kept alive at the same time, before thread-related issues arise;
  4. I suspect that the smack library has its own issues with regard to Presence;
  5. I have not checked  for memory leakages, I use nested classes for my BroadcastReceivers, I haven't checked for cyclical references, I don't understand fully how garbage collection on android works;
  6. The vibrator notification can get annoying, or fun, depending on your mood.

Tuesday, December 13, 2011

XSS techniques: defeat a target that only escapes single and double quotes

I assume that you found an entry point either, from
  1. the url
  2. an injectable
    <input .../>
  3. or a form input, find functions on certain websites are very exploitable
Assume that the target accepts injections with slashes and escapes only single and double-quotes as a security measure:

Try to break the webpage with '-->', e.g. html comment termination,
inject your payload, e.g. <script>...</script>

Escaping double and single quotes:

a.
(new String(1)).replace((new Number(1)).toString(10), /look no single or double quotes/);

b.
String.fromCharCode(n1, n2, ..., nX)

c. replace (for compression)

Use this python snippet to prepare the payload:

[ ord(c) for c in "http://payload_in_decimal" ]

The drawback of this technique is that the decimal payload will be huge and it might overflow the input, i.e. url input on the browser, input-tag, form etc.

Friday, July 22, 2011

Song lyrics memorization using method of loci

Sometimes when studying to play a song on guitar or bass it's difficult enough to memorize chord changes, let alone singing along. This becomes more difficult if there are any syncopations or some rhythmic off-beat device in the song. In some songs some words in the whole chorus are different each time in a different bar, but you just can't manage to memorize which words belong to which repetition of the chorus. I propose a remedy for this problem.


First the muscle memory must already be familiar with the chord progressions, if you're compin' along to the melodies, the chords could be anchored to the lyrics and melody of the song.


Use the mnemonic device called the method of loci. The advantage of this method is that you can jump between from verse and chorus by just stepping into the room you've associated the verse or chorus with (in your mind).
Use real objects in the room, that are usually there, (e.g. tv, couch, rug in the living room, and toilet seat, shower in the bathroom) to anchor an initial strand of a story. The story is just a sequential translation of the lyrics into memorable narrative. The more outrageous, the more memorable.
If you speak multiple languages use translations of words and the phonetic information to link words together.



Manipulate the object translation of words in the lyrics in the space e.g. explosions, actions etc. Use a phonetic approximation of the word if it isn't an easily translatable object. It's important to anchor to existing objects in the space, e.g. tv, couch, toilet, showerhead, cupboard, dining table etc. One could also use synonyms but this would tend to require some thought (introduce a new layer of translation), which would take extra time.
If you speak multiple languages use translations of words and the phonetic information to link words together.

It's key to reduce the recall-time, to stay in the rhythm of the song, even before you actually need the information.
I discovered a trick to optimize on recall time, by grouping triggers together for the next word which fall on the start of the musical phrase, before you actually need the trigger. For example, you need to recall B in the following melodic phrase, you need A in the current melodic phrase, so you chunk mnemonic triggers for AB together, before you actually need B. You could also memorize a meta-trigger for B, instead of the real trigger for B.


It's important to walk through (in the mind) the rooms, during practice, as if you're walking through different parts of the song, so the sequence of the verse, bridge, chorus is easy to traverse. I use my friends homes to memorize songs.

For instance a pop song of 2 minutes, there would be at most 3 distinct lyrical parts e.g. verse, chorus, bridge. If certain parts occur together like two verses followed by a chorus, this could be chunked together in one room, then the rest could be chunked together in the following room; a song could theoretically require two rooms. Two songs could theoretically could fit into a locus of 5 rooms, mnemonically speaking.
You can also try to engage your limbic brain in memorizing the lyrics, by imagining the sensations produced by the lyrics. You can imagine feeling a warmth on your cheeks, when you sing the word "Hot" etc.

It's also good to use multiple non-conflicting triggers (e.g. sensations, images, narrative, onomotopeia, synonyms), in your story to encode the lyrics, eventually the strongest trigger will remain.

It's fairly obvious that one should also practice recalling the triggers.

Monday, March 14, 2011

Fun with a whiteboard

IT worker with a mind like water

Introduction

In this entry, I'll be proselytizing about my interpretation of Getting Things Done (GTD) by David Allen, mindfulness and my implementation of Agile principles.

All of this culminates in a project portfolio, for a high-level view of the project status.

Quick reference to GTD

GTD principles and mindfulness

In order to work at maximum efficiency in bursts during the day, one must maintain a mind which flows like water. IT-workers, like physical athletes, chess champions etc. must keep their cool and maintain a certain level-headedness to perform at the top of their game. An IT-worker can get to this state by practicing mindfulness excercises during down-time and implement GTD in their personal and professional lives.

Why GTD? Because it is a means to cast away projects and tasks and entrust them to a externalized system, so there would be no need to retain excess information in the mind. So your mind is free to react to or observe the situation. I'll write up a blog entry to illustrate some points made by cognitive studies which support the beneficial effects of the GTD system.

Why mindfulness? Because it's the new fad. No, not really, meditation has been with us for a long time, but meditation is only a means to an end.

Mindfulness can potentially relieve (not a 100% cure) certain physical manifestations of mental blockages (I'm not a dualist in the cartesian tradition). Like all other things in life, you need a time and a place to do this and practice. So that's taking care of the body/mind. Regular physical conditioning/exercise  is also a good idea. I won't cite the hundreds of research papers which confirm the relation between being mentally fit and physical exercise.

Meditating Lotus


A common goal mindfulness and GTD is to empty the mind of all baggage and to get in a relaxed control of the task at hand, with minimum interference from other senses. Even if you think it's all weird esoteric unscientific crap, there's always a possibility of a positive placebo effect on your mind. So try it.

David Allen also stresses the importance of setting positive goals. This creates a positive feedback loop, when things actually don't turn out like the worst-case scenario you initially envisioned.

Teams and Agile and GTD

I could imagine that the other team members might not fully appreciate your fascination with GTD or mindfulness. So lead by example.

So, how does one share a mind-like-water epiphenomenal state with your colleagues? Grab adhesive pieces of paper (e.g. stick-it), get a large sheet of paper or a whiteboard, start writing down all of the project details as a group. Group together similar tasks / details. Find a comfortable granularity i.e. level of description for the tasks for the whole team. Not too abstract and not too detailed. Unload your team's collective mind. This is a free-flowing brainstorm / brain unload state.  The unloading state can be done over a week or in 5 minutes. Depending on the size of the project. I personally did a drive-by braindump with my team once.

Braindump / fun with stick-its



The whiteboard


Get together with your team. Your role is to provide the constraints of the project. Per task, sort the precedence of certain tasks depending on the priority of the task. This is a filtering state. Make a plan in iterations for the coming x weeks. Every iteration should be no longer than 2 weeks. For every task, subproject, try to make a informed guess how long that task would take to accomplish. Also try to define the priority of that feature with the client and your teammates. It's important to make a clear seperation between the "unloading" state and the filtering / analytical / censor state.

If management insists on disrupting you or team members to do multi-project multi-tasking, use GTD on a lower-level for each disruptable team member. Use your project portfolio and project backlog to defend your team member, against unwelcome intrusions like these, for example make your point clear to "intruders" that your project has no time to spare etc. In any case a project portfolio is a common base you can use to negotiate, within the team or with upper-management.

Spreadsheet based project portfolio, left: monthly plan, right: weekly plan.


Another method to maintain a team's commitment during a barrage of incidental crap, use a kanban to keep the focus on the project at hand. Also, use commitment and consistency. Let the person(s) who is or are going to be responsible for the task write down her name on the piece of paper.

Simple KanBan: wait, active, finished.


For software developers you might want to describe the following states on the kanban (per feature) for an iteration:
  1. Develop
  2. Test
  3. Refactor
  4. Merge
  5. Commit to version control
  6. Integration Test
  7. Deploy
Listen to your team members, to find the right kanban which works for everyone. Update the kanban when required.

Depending on the size of the team, do standups. Do project backlogs, so you can make a project portfolio, do a velocity charts. But in any case never plan alone, always use the data provided by the whole team.

My software idea

I envision a drag and drop interface to create an online kanban system, where states can be defined. Using xmpp and bosh to give project members updates in changes within the kanban system. Integration with XMPP-based chat systems. All of this integrated with a calendar (gcalendar?) and a GTD based system where tasks can be delegated to people and be inserted to the wait list or the calendar.  Ad hoc commands are in the protocol to deliver the data payload.

Conclusion

There is a common thread between agile and GTD. If you include team members and let them actively participate, you can leverage the group's wisdom to improve commitment to the project and also improve the way it is going to be developed. This is a way for your team to get into the zone.