Using JSON Data as Pitch Generator

Home

This tutorial will show you, how you can get data from a simple RESTful webservice and generate sound from it.

In this tutorial, I will use the following notation:
[my object]: an object with the name "my-object".
[my own message(: a message with the content "my own message".

Getting the Data

This example gets the daily trading prices of Bitcoin. In its current implementation, it takes the data from Bitcoin Average, and translates the data from csv to JSON.

The patch

After designing the data online, we query the webservice with a GET request using [rest].

The data then looks something like this:

[
    {"DAY":"2010-07-17","USD":0.05,"VOLUME":20},
    {"DAY":"2010-07-18","USD":0.07,"VOLUME":75.01},
    {"DAY":"2010-07-19","USD":0.09,"VOLUME":574},
    {"DAY":"2010-07-20","USD":0.08,"VOLUME":262}
]

When the data is received, the returned value is fed into [json-decode]. As the symbol atom from [rest] will be a JSON array, we will get messages for each JSON object on the left outlet followed by a bang on the right outlet.

A sequence of messages for an object looks like this:

list DAY 2012-09-22
list USD 12.2
list VOLUME 16883.95

Processing the Data

This section discusses the operations in the subpatch [pd process-data].

pd process-data

In this subpatch, the incoming lists from [json-decode] are taken and packed into one list for each JSON object in the array.

First, we remove the list prefix from each message, then pack those messages, and use the bang message from the right outlet of [json-decode] that is emitted after each decoded array member to trigger the output of [pack]. The transformed message for each JSON object then looks like:

0 4.8743 2011-09-30

Each output from put starts with a 0, as we only use the bang on the first inlet of [pack] to trigger the output. We also discard Euro rates and only care for US Dollar.

We then use [fifop] from zexy to store the values. This is FIFO buffer, that advances on bang.

After the data is output from [fifop], the list is unpacked and connected to seperate outlets. Note: We discard the leading 0.

Stepping through each values

This section discusses the operations in the subpatch [pd stepper].

pd stepper

This subpatch starts a [metro] after all data is stored in the [fifop] inside [pd process-data]. When [fifop] is done, the bang from the right outlet of it is used to stop the [metro].

Generating sound

Sound generation is the bottom part of the main patch.

The exchange rate for Bitcoins vary widely, with values ranging from ca. 3 US$/Bitcoin to ca. 800 US$/Bitcoin. To make any sense of the exchange rates, and especially to get some short term variation in pitch change, scaling the data logarithmically makes sense.

Different data might benefit from other scaling methods, linear or exponential.