# Saving from :sound\_in to samples

**URL:** https://in-thread.sonic-pi.net/t/saving-from-sound-in-to-samples/2430
**Category:** Feature Requests
**Created:** [May 29, 2019, 7:56am UTC](https://in-thread.sonic-pi.net/t/saving-from-sound-in-to-samples/2430 "2019-05-29T07:56:20Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![siimphh](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/siimphh/32/1851_2.png) [@siimphh](https://in-thread.sonic-pi.net/u/siimphh)
#### Post date: [May 29, 2019, 7:56am UTC](https://in-thread.sonic-pi.net/t/saving-from-sound-in-to-samples/2430/1 "2019-05-29T07:56:20Z")

</div>

I would like to experiment with sonic pi for “live looping” (the other kind of live loop), that is recording audio input into samples and playing them back.

I believe the live looping tools are usually pretty fancy in allowing some sequencing (and effects of course) on recorded clips and it’s a pretty cool way of starting making fun sounds without requiring a lot of resources.

WDYT?

---

<div class="post-metadata">

### Author: ![perpetual\_monday](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/perpetual_monday/32/955_2.png) [@perpetual\_monday](https://in-thread.sonic-pi.net/u/perpetual_monday)
#### Post date: [May 29, 2019, 2:51pm UTC](https://in-thread.sonic-pi.net/t/saving-from-sound-in-to-samples/2430/2 "2019-05-29T14:51:35Z")

</div>

A couple examples worth looking at:

> **[mbutz/sonic-pi-midi-live-looper](https://github.com/mbutz/sonic-pi-midi-live-looper)**
>
> A Live Looping Script for Sonic Pi and Midi Devices - mbutz/sonic-pi-midi-live-looper

> <https://gist.github.com/rbnpi/de5204e4ff68b7f1992a32b92056caf6>

The second example does not do live audio looping, but is good to learn from and could be adapted to handle live audio.

---

<div class="post-metadata">

### Author: ![siimphh](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/siimphh/32/1851_2.png) [@siimphh](https://in-thread.sonic-pi.net/u/siimphh)
#### Post date: [May 29, 2019, 10:55pm UTC](https://in-thread.sonic-pi.net/t/saving-from-sound-in-to-samples/2430/3 "2019-05-29T22:55:06Z")

</div>

Very cool, thanks for the references! They definitely helped with figuring out how to use `record` and `buffer` objects together! The language reference could use an example or two, I think 🙂

Based on the examples, I was trying something like this:

```ruby
if not defined?($takes) then
  $takes = {}
end

# Record or play a buffer from sound input
# Params:
# - n: name of the `buffer` to use
# - l: length (in beats) of the `buffer` to use
# - t: take number -> re-recorded if greater than the last recorded take
def brp(n, l, t)
  k = [n, l]
  b = buffer n, l
  if t == 0 then
    # Just audition, don't play the last take, don't record
    synth :sound_in, sustain: l
    sleep l
  elsif ($takes[k] or 0) >= t then
    # Play the last recorded take
    sample b
    sleep l
  else
    # Record another take (while playing it back)
    $takes[k] = t
    with_fx :record, buffer: b do
      synth :sound_in, sustain: l
      sleep l
    end
  end
end

live_loop :baseline do
  sync :tock
  with_fx :octaver do
    brp :baseline, 4, 4
  end
end

live_loop :melody do
  sync :tock
  brp :melody, 8, 4
end

```

I didn’t really have time to play with it much (it’s late already), but this interface feels promising to me. The idea being that the `brp` can be chucked in anywhere `sample` or `synth` could and it keeps on repeating the last `take` until you increment the take number for one of them to re-record it.
