# Initializing CsoundObj in Web Csound

**URL:** <https://forum.csound.com/t/initializing-csoundobj-in-web-csound/136>\
**Category:** Web Csound\
**Created:** [February 2, 2021, 3:00pm UTC](https://forum.csound.com/t/initializing-csoundobj-in-web-csound/136 "2021-02-02T15:00:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JasonHallen](https://forum.csound.com/user_avatar/forum.csound.com/jasonhallen/32/32_2.png) [@JasonHallen](https://forum.csound.com/u/JasonHallen)\
**Post date:** [February 2, 2021, 3:00pm UTC](https://forum.csound.com/t/initializing-csoundobj-in-web-csound/136/1 "2021-02-02T15:00:33Z")

</div>

Hi,

I’m seeing two ways people are initializing CsoundObj in Web Csound.

1. `CsoundObj.importScripts("./csound/").then(() => {setupCsound();});` ([example here](https://github.com/kunstmusik/csound-p5js/blob/master/csound-p5js.js))
2. `CsoundObj.initialize().then(() => {startCsound();});` ([example here](https://editor.p5js.org/rorywalsh/sketches/qlcIDwkEW))

Can someone explain the difference between the two in terms of how they initialize CsoundObj?

Thanks!  
Jason

---

<div class="post-metadata">

**Author:** ![stevenyi](https://forum.csound.com/user_avatar/forum.csound.com/stevenyi/32/4_2.png) [@stevenyi](https://forum.csound.com/u/stevenyi)\
**Post date:** [February 2, 2021, 3:31pm UTC](https://forum.csound.com/t/initializing-csoundobj-in-web-csound/136/2 "2021-02-02T15:31:46Z")

</div>

importScripts() was what was in the API for a long while, but I modified it in 6.15.0 to initialize() and added the ability to send in an AudioContext to use, which was important for making the Csound nodes work in a larger Webaudio graph.

That said, the new version of WebAudio Csound has undergone many changes for 6.16.0 and the process has changed again. New code looks like:

```csound
const cs = await Csound(); // may pass in arguments as object to Csound

```

Or since it is a promise you can use:

```csound
Csound().then(() => {
});

```

6.16.0-beta is already published to NPM for testing though we are waiting for a few more changes to publish beta4, which we’ll then announce for testing. We’ll have the above documented in tutorials.

---

<div class="post-metadata">

**Author:** ![JasonHallen](https://forum.csound.com/user_avatar/forum.csound.com/jasonhallen/32/32_2.png) [@JasonHallen](https://forum.csound.com/u/JasonHallen)\
**Post date:** [February 2, 2021, 3:37pm UTC](https://forum.csound.com/t/initializing-csoundobj-in-web-csound/136/3 "2021-02-02T15:37:17Z")

</div>

Fantastic, thanks for the explanation, Steven! Makes sense. I’m looking forward to 6.16.0!

Jason

---

<div class="post-metadata">

**Author:** ![boonier](https://forum.csound.com/user_avatar/forum.csound.com/boonier/32/426_2.png) [@boonier](https://forum.csound.com/u/boonier)\
**Post date:** [July 26, 2024, 12:58pm UTC](https://forum.csound.com/t/initializing-csoundobj-in-web-csound/136/4 "2024-07-26T12:58:59Z")

</div>

Hello!

I’m trying to connect to the audio context created by my custom op in Cables.gl

```csound
const cablesAudioContext = CABLES.WEBAUDIO.createAudioContext(op);

const cs = await Csound({
        audioContext: cablesAudioContext,
});

```

Should the above be sufficient?

I get the Csound orc/score playing, but it seems to be playing though it’s own audioContext, not the one that I’ve passed to the Csound constructor. (this is verified by adjusting levels downstream of the custom op in Cables)

thanks

---

<div class="post-metadata">

**Author:** ![stevenyi](https://forum.csound.com/user_avatar/forum.csound.com/stevenyi/32/4_2.png) [@stevenyi](https://forum.csound.com/u/stevenyi)\
**Post date:** [July 26, 2024, 2:22pm UTC](https://forum.csound.com/t/initializing-csoundobj-in-web-csound/136/5 "2024-07-26T14:22:10Z")

</div>

Seems like you need to connect csound node to a target node in Cable’s audio graph. By default, csound will connect directly to the audioContext. You can see an example of re-routing the signal to another webaudio node here:

> <https://github.com/kunstmusik/sound-bath/blob/main/src/App.tsx#L59-L68>

---

<div class="post-metadata">

**Author:** ![boonier](https://forum.csound.com/user_avatar/forum.csound.com/boonier/32/426_2.png) [@boonier](https://forum.csound.com/u/boonier)\
**Post date:** [July 30, 2024, 11:45am UTC](https://forum.csound.com/t/initializing-csoundobj-in-web-csound/136/6 "2024-07-30T11:45:43Z")

</div>

Brilliant - creating the gainNode was the key (I was reconnecting to the context destination i.e the speakers everytime):

[https://cables.gl/p/yGSsXC](https://cables.gl/p/yGSsXC)

Thanks

---

<div class="post-metadata">

**Author:** ![stevenyi](https://forum.csound.com/user_avatar/forum.csound.com/stevenyi/32/4_2.png) [@stevenyi](https://forum.csound.com/u/stevenyi)\
**Post date:** [July 30, 2024, 6:08pm UTC](https://forum.csound.com/t/initializing-csoundobj-in-web-csound/136/7 "2024-07-30T18:08:31Z")

</div>

Great to hear that worked out. BTW: The patch is private (I sent a request for access). 🙂

---

<div class="post-metadata">

**Author:** ![boonier](https://forum.csound.com/user_avatar/forum.csound.com/boonier/32/426_2.png) [@boonier](https://forum.csound.com/u/boonier)\
**Post date:** [August 2, 2024, 11:32am UTC](https://forum.csound.com/t/initializing-csoundobj-in-web-csound/136/8 "2024-08-02T11:32:40Z")

</div>

Sorry - you should have access now! Does it work for you? First time sharing a patch 🙂

Cheers!

---

<div class="post-metadata">

**Author:** ![stevenyi](https://forum.csound.com/user_avatar/forum.csound.com/stevenyi/32/4_2.png) [@stevenyi](https://forum.csound.com/u/stevenyi)\
**Post date:** [August 2, 2024, 1:58pm UTC](https://forum.csound.com/t/initializing-csoundobj-in-web-csound/136/9 "2024-08-02T13:58:03Z")

</div>

Works now, nice demo!
