Skip to content
This repository has been archived by the owner on Nov 18, 2023. It is now read-only.

Using JSON Data as Pitch Generator

Thomas Mayer edited this page Nov 20, 2022 · 4 revisions

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 conversion rates between US Dollar und Euro from the European central bank, which on the server is converted from XML to JSON.

The patch

The data from the script on the website looks something like this:

[
    {"day":"1999-01-04","rate":1.1789},
    {"day":"1999-01-05","rate":1.179},
    {"day":"1999-01-06","rate":1.1743},
    {"day":"1999-01-07","rate":1.1632}
]

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 middle outlet followed by a bang on the left outlet.

A sequence of messages for an object looks like this:

list day 1999-01-04
list rate 1.1789

Storing the Data

This section discusses the operations in the subpatch [pd store-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 left 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 1999-01-04 1.1789

Notes:

  • The date will not come out as a symbol. Conversion is done with a [symbol] object.
  • Each output from [pack f s f] starts with a 0, as we only use the bang on the first inlet of [pack] to trigger the output.

We then use [text] to store the value. We use a very high number for the position 1e+15, to ensure that new values are appended without a counter before.

Sequencing the values

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

pd stepper

This subpatch starts a [metro] after all data is stored. This is basically just the sequencer example from the [text] help file.

Generating sound

Sound generation is the bottom part of the main patch.

The exchange rates between US Dollar and Euro is varying very narrowly around 1. The value is multiplied by a fixed number and with an offset interpreted as MIDI notes, such that a change of 0.03 in the exchange rate will result in a change of the tone of 1 semitone.

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