# Question on \`\`\`insertWaveClip()\`\`\`

**URL:** <https://forum.juce.com/t/question-on-insertwaveclip/48103>\
**Category:** Tracktion Engine\
**Created:** [October 4, 2021, 3:33pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103 "2021-10-04T15:33:22Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [October 4, 2021, 3:33pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/1 "2021-10-04T15:33:22Z")

</div>

I am having issues with importing audio clips with `insertWaveClip()`. Sometimes the clip imports just fine. Sometimes it is just silence. Sometimes it is wildly distorted. Sometimes it doesn’t import at all. And the clips are known good audio files, all of which were created by my DAW, and otherwise play back normally.

I am almost certainly not using `insertWaveClip()` properly.

My code is below…

```auto
File file{ editViewState.audioFilePath };
FileChooser fileChooser("Open Audio File", file, "*.wav");
if (fileChooser.browseForFileToOpen())
{
	file = fileChooser.getResult();

	const auto clipTrack{ dynamic_cast<te::ClipTrack*>(trackPtr) };
	te::AudioFile audioFile{ trackPtr->edit.engine, file };

	const auto sampleRate{ transport.engine.getDeviceManager().getSampleRate() };
	const auto audioFileSampleRate{ audioFile.getSampleRate() };

	if (audioFileSampleRate != sampleRate)
	{
		AlertWindow::showMessageBoxAsync(MessageBoxIconType::WarningIcon, "Import Audio Error", "FileSampleRate= " + String(audioFileSampleRate) + "\nEditSampleRate= " + String(sampleRate));
	}
	else
	{
		const auto clipLength{ audioFile.getLength() };
		const auto start{ transport.getCurrentPosition() };
		clipTrack->insertWaveClip(file.getFileNameWithoutExtension(),file,{{start, clipLength}, 0.0}, false) ;
	}
}

```

Some suggestions and guidance is most appreciated.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [October 5, 2021, 1:57pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/2 "2021-10-05T13:57:11Z")

</div>

The only dodgy thing I can see is that `{start, clipLength}` for the `EditTimeRange`. That has start and end times so should probably be `{start, start + clipLength}`?

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [October 5, 2021, 3:44pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/3 "2021-10-05T15:44:00Z")

</div>

I modified the bottom section of the code above to be as follows;

```auto
const auto clipLength{ audioFile.getLength() };
if (const auto waveAudioClip{ clipTrack->insertWaveClip(file.getFileNameWithoutExtension(),file,{{0.0, clipLength}, 0.0}, false) })
{
	const auto start{ transport.getCurrentPosition() };
	waveAudioClip->setStart(start, true, true);
}

```

This code seems to make importing a bit more consistent, However, importing of some files, in particular my renders, visually creates a solid block of color and plays back as a loud buzz.

I am not sure what to try next?

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [October 5, 2021, 4:19pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/4 "2021-10-05T16:19:52Z")

</div>

It could be that they’re being time stretched and that’s not working. If they’re exports, are they WAV? Presumably they’ll have tempo metadata in them then.

What time stretch engine are you using? Can you see if a proxy file is being created for them?

Also, break in WaveNode and see what file is actually getting played back. If you open that on your OS, is it also junk?

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [October 5, 2021, 6:26pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/5 "2021-10-05T18:26:25Z")

</div>

Well that is what is so puzzling,…they are just plain WAV files. They look and play back completely normally in other contexts! For example, the rendered file that when imported is visually a solid block, and produces a loud buzz, looks and plays back perfectly in Audacity.

I do not do any time stretching.

In some more testing, it seems that WAV files created as clips in a track, will import as intended now. So, it seems that it is just the rendered files that import incorrectly. So, perhaps there is a difference in how rendered files are treated for export?

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [October 5, 2021, 6:48pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/6 "2021-10-05T18:48:17Z")

</div>

Can you try loading the files in to Waveform? Do they play there?

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [October 5, 2021, 7:03pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/7 "2021-10-05T19:03:36Z")

</div>

I can try Waveform later in the studio. But, I do have Tracktion 7 installed on my development computer, and the file imports correctly in Tracktion 7. It looks normal, and plays back normally.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [October 5, 2021, 8:18pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/8 "2021-10-05T20:18:43Z")

</div>

Sis you try breaking in the WaveNode constructor like I suggested to check that it’s the same file that’s being played back and not a proxy that’s been generated?

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [October 5, 2021, 9:12pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/9 "2021-10-05T21:12:35Z")

</div>

With a breakpoint in the `WaveNode` constructor, the file path is as below;

```auto
fullPath	{text={data=0x000001943ed533f0 "C:\\Users\\User\\AppData\\Roaming\\TheDAW\\Temporary\\edit_0_41e6d\\clip_0_1046_1dd4129a24eee8c2.wav" } }	juce::String

```

…which appears to me to be the clip name that the file will be imported to. Is that correct?

There is otherwise no mention, that I can find, of the imported file, which is simply named “Render.wav”.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [October 5, 2021, 11:04pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/10 "2021-10-05T23:04:35Z")

</div>

Well it looks like it’s generating that clip file from your original. I’m not at my computer at the moment and can’t remember off hand what the “clip” prefix is for but it could be looped or a proxy…

Does that file play back on the OS or is it garbage too?

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [October 6, 2021, 2:05pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/11 "2021-10-06T14:05:06Z")

</div>

The temporary file is garbage also.

What is really strange is that sometimes the import works perfectly!? And everything is the same. In other words, all of the files were created by my DAW, which is totally JUCE and TracktionEngine based. And all the files play normally in other contexts such as Windows Media Player or Audacity (except for the temporary mentioned above).

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [October 6, 2021, 2:41pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/12 "2021-10-06T14:41:44Z")

</div>

Well I’ve set this several times but what’s most likely happening is that when you exported these files they have the tempo and pitch metadata they were exported from embedded in the files. This is to be expected as it enables you to tempo/pitch match them when importing them.

So when you import the files, Tracktion Engine tries to create a proxy file to match the source file tempo/pitch to the Edit’s tempo/pitch. If you put a breakpoint in `AudioClipBase::getProxyFileToCreate` you’ll see this triggered.

It sounds like if you don’t have SoundTouch or Elastique timestretching enabled, junk is getting created. Tbh I’m not sure what it should do in these situations, you really should have one time stretch mode enabled.

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [October 6, 2021, 2:55pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/13 "2021-10-06T14:55:54Z")

</div>

I was not aware the time stretch was required.

Since I have no need of it, what is the most benign way to invoke time stretch with the least impact on the program?

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [October 6, 2021, 3:07pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/14 "2021-10-06T15:07:31Z")

</div>

Probably just disable auto-tempo and auto-pitch on newly added clips?  
I think that would do it.

But I should also add an assertion or similar to the time stretching render code to catch this case.

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [October 6, 2021, 3:44pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/15 "2021-10-06T15:44:45Z")

</div>

I was going to ask how to disable auto-tempo and auto-pitch, then I found `setTimeStretchMode()`, and I was going to use;

```auto
waveAudioClip->setTimeStretchMode(te::TimeStretcher::disabled);

```

But then I also noticed that “disabled” is the default. So, that will not change anything.

So, what is the way to disable auto-tempo and auto-pitch?

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [October 6, 2021, 3:51pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/16 "2021-10-06T15:51:23Z")

</div>

I think I found it!

```auto
waveAudioClip->setAutoPitch(false);
waveAudioClip->setAutoTempo(false);

```

I will try this out.

Please let me know if there is anything else I need to change?

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [October 6, 2021, 4:01pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/17 "2021-10-06T16:01:31Z")

</div>

Adding the lines below seems to have fixed the issue.

```auto
waveAudioClip->setAutoPitch(false);
waveAudioClip->setAutoTempo(false);

```

I will let you know if I run into any issues.

I would argue that auto-tempo and auto-pitch should default to “false”.

Are there any other unusual requirements I might be missing?

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [October 6, 2021, 9:06pm UTC](https://forum.juce.com/t/question-on-insertwaveclip/48103/18 "2021-10-06T21:06:42Z")

</div>

I will do more testing, but the code below is currently working for doing successful audio imports.

```auto
File file{ editViewState.audioFilePath };
FileChooser fileChooser("Open Audio File", file, "*.wav");
if (fileChooser.browseForFileToOpen())
{
	file = fileChooser.getResult();
	
	const auto clipTrack{ dynamic_cast<te::ClipTrack*>(trackPtr) };
	
	te::AudioFile audioFile{ trackPtr->edit.engine, file };
	const auto sampleRate{ transport.engine.getDeviceManager().getSampleRate() };
	const auto audioFileSampleRate{ audioFile.getSampleRate() };

	if (audioFileSampleRate != sampleRate)
	{
		AlertWindow::showMessageBoxAsync(MessageBoxIconType::WarningIcon, "Import Audio Error", "FileSampleRate= " + String(audioFileSampleRate) + "\nEditSampleRate= " + String(sampleRate));
	}
		else
	{
		const auto clipLength{ audioFile.getLength() };
		if (const auto waveAudioClip{ clipTrack->insertWaveClip(file.getFileNameWithoutExtension(),file,{{0.0, clipLength}, 0.0}, false) })
		{
			const auto start{ transport.getCurrentPosition() };
			waveAudioClip->setStart(start, false, true);
			waveAudioClip->setAutoPitch(false);// needed to disable pitch shift
			waveAudioClip->setAutoTempo(false);// needed to disable time stretch
		}
	}
}

```

Thank you for the help!
