# Trigger Tidal code with MIDI

**URL:** <https://uzu.lurk.org/t/trigger-tidal-code-with-midi/2168>\
**Category:** TidalCycles\
**Tags:** innards\
**Created:** [16 September 2020 17:11 UTC](https://uzu.lurk.org/t/trigger-tidal-code-with-midi/2168 "2020-09-16T17:11:15Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![mrreason](https://uzu.lurk.org/user_avatar/uzu.lurk.org/mrreason/32/3203_2.png) [@mrreason](https://uzu.lurk.org/u/mrreason)\
**Post date:** [20 September 2020 10:07 UTC](https://uzu.lurk.org/t/trigger-tidal-code-with-midi/2168/19 "2020-09-20T10:07:24Z")

</div>

> [@Xon](#):
>
> Your example works great.

Glad to hear! So let's try to answer your questions:

> [@Xon](#):
>
> 1. To understand better your code and syntax, do you know a particular Haskell ressource that explains `capply`, especially `''` ?

I wrote three different `capply` functions, named `capply`, `capply'`and `capply''`. These are just valid function names. It would be totally fine to name the `capply''` function `thisismyawesomefunction` or something like that...But I guess you should get the idea.

> [@Xon](#):
>
> 1. When I try to select and evaluate all the code at once, I have parse errors:  
> -\> possibly incorrect indentation or mismatched brackets  
> -\> error: expecting a single import declaration  
> Do you know how to avoid to evaluate the different parts of the code in several times?

You can copy the functions in your BootTidal.hs or in a separate file. You could load a separate file manually with

```haskell
:script mytidalfuncs.tidal

```

But make sure that you wrap multiple lines with :{ and }: . I already saw an example of this in the forum:

> [@Superfm](https://club.tidalcycles.org/t/superfm/1761/14):
>
> You need to make the loaded script into a multiline command by wrapping it in :{ :} like this: :{ -- sets the amount of operator 'op' in the superfm output mix -- (1 \<= op \<= 6) fmamp :: Int -\> Pattern Double -\> ControlPattern fmamp op = pF ("amp" ++ show op) -- sets the ratio for operator 'op'. -- the frequency is note \* ratio + detune Hz -- (1 \<= op \<= 6) fmratio :: Int -\> Pattern Double -\> ControlPattern fmratio op = pF ("ratio" ++ show op) -- set th…

> [@Xon](#):
>
> I've realized it would be great to be able to have different kinds of transitions or not.

I think I now understand the requirements for a one-size-fits-all solution.  
This could be a larger project and I would prefer to exclude this to an issue on Github to discuss it there. Let me try to explain this. What you could do with the custom `capply` function is to switch between patterns conditionally with a pattern like:

```haskell
d1 $ capply'' "<0 1 2 3>" pvector 

```

This works totally fine. And the osc control messages are basically patterns. It is not possible (or I don't know it yet) to control (let's call it) IO function like transitions (like `xfade`) or `d1`, `d2` with patterns. What you could do for example is switching the output bus with a function like orbit:

```haskell
-- Some kind of switching between d1 and d2
d1 $ s "808*4" # orbit "<0 1>" 

```

I see two possible solutions here:

1. There should be the possibility to control transitions with patterns and use them similar to the function `orbit`. Then my solution for selecting patterns with patterns would also work on transitions.
2. Or we need a way to trigger IO functions through OSC. But I think that we then leave the pattern context and I'm not sure if this is intended.

I can imagine that you are more flexible with this second approach. This would allow to trigger the IO-functions and the attached patterns independently from the TidalCycles clock. I think of it as a variant of my CycSeq approach, but then built into TidalCycles. In CycSeq I have code in a separate window and I activate a window with the code it contains. This is basically nothing else than having an osc controller that's wraps TidalCycles code which you could trigger from outside the IO functions.  
But you see, that this contains a more in depth discussion.

[Edit:] I actually see that there is an issue for trigger IO functions with pattern on GitHub: [IO patterns · Issue #483 · tidalcycles/Tidal · GitHub](https://github.com/tidalcycles/Tidal/issues/483) Maybe this is a feasible approach. But as far as I see, this is using an IO-function like reading a file to interpret this as a pattern. But maybe this works for triggering a transition like `xfade` inside `d1`...

But I would also be really happy if we could find a solution.  
In my case, I can imagine that this will give me an easier environment in TidalCycles to do live sampling/live looping.

---

_[View the full topic](https://uzu.lurk.org/t/trigger-tidal-code-with-midi/2168)._
