Cabbage Logo
Back to Cabbage Site

Using buttons to explore data

Hi, I’m rather new to Csound, and Cabbage. I’m currently attempting to just work through any examples and see how far I can get with it. I have several queries about something I’m trying to work on.

My first query is how to get a sound to be output when a button is pressed? And once I know this, I’m looking to read data, and have the sound output change depending on the relationship of a data set.

My second query is what type of instrument would be good to use if I’m looking to play a continuous note as a data series is read from a file or a table? Also, is there a way of playing specific chords(?), and does it depend on the type of instrument wanting to be output?

My final query is, is there a way of creating an option where different files can be read into the program? So instead of entering the data into a function table and having to change it inside the code, is there a way of essentially uploading it to be read?

Any assistance would be great!:slight_smile:

The simplest way would be to create an instrument that is always playing and then simply test if the button is on or off. For example, if instr 1 is always playing (i1 0 z in the score), it will check the value of the button with channel button1 on each k-cycle. If it is on it will enter the if block of code.

instr 1
   if chnget:k("button1") == 1 then
   //make some sounds here

It’s very hard to answer that as I have no idea what type of sound you wish to create. A simple sine wave can play continuous data for as long as you like. Any number of synthesis techniques can be used in this situation.

Chords can be created by layering instances of instruments on top of each other. For example, this example will play a chord each time one presses a button.

instr 1
   kButtonValue chnget "button1"
   if changed(kButtonValue) == 1 then
      event "i", 2, 0, 2, 60
      event "i", 2, 0, 2, 64
      event "i", 2, 0, 2, 67

instr 2
   aEnv madsr .1, .4, .4, .1
   a1 oscili .25, cpsmidinn(p4)
   outs a1*aEnv, a1*aEnv

Note that instrumnt 1 is started from the score, but instrument 2 is triggered to play from instrument 1.

There are a number of file reading opcodes in Csound that you can explore. It depends to a large extent on how the data is presented in the file you are trying to read from. Take a look at some of these opcodes.

Let us know how you get on.

Try the following files. Just make sure that dataSet.txt is in the same directory as the .csd file. The dataSet.txt file just contains numbers between 1 and 3. Instrument one read one of these numbers every second using readk.

instr 1
   kButtonValue chnget "button1"
   if kButtonValue == 1 then
      kValue readk "dataSet.txt", 7, 1
      if changed(kValue)==1 then   
         printk2 kValue   
         event "i", 2, 0, 1.2, kValue

The value of the umber is used to drive the frequency of a sine wave oscillator, found in instrument 2. Hopefully this will get you going. As I said before, a lot will depend on the format of your data set.

buttonPress.csd (925 Bytes)
dataSet.txt (72 Bytes)

I have deleted previous posts, as I have now done what I was intending to do. Thanks for all your help Rory! I will surely be back soon to continue my journey in Csound and Cabbage!:slight_smile: