- Home /
Store AudioClip with pitch
I have an app where users can play around with the pitch of an AudioSource. The next thing i want is the users to save the AudioClip to load it later and continue with the sounds. Without the pitch this is not a problem and i can now save an AudioClip as a .wav file in storage (code below). What it does is just save the data of the AudioClip, so it does not take the pitch in consideration (there is some more code but i think this is the most important stuff).
How can i manipulate the data to make sure it is being stored together with pitch?
//TODO: static void ConvertAndWrite(FileStream fileStream, AudioClip clip, float pitch) {
static void ConvertAndWrite(FileStream fileStream, AudioClip clip) {
var samples = new float[clip.samples];
clip.GetData(samples, 0);
Int16[] intData = new Int16[samples.Length];
//converting in 2 float[] steps to Int16[], //then Int16[] to Byte[]
Byte[] bytesData = new Byte[samples.Length * 2];
//bytesData array is twice the size of
//dataSource array because a float converted in Int16 is 2 bytes.
int rescaleFactor = 32767; //to convert float to Int16
for (int i = 0; i<samples.Length; i++) {
intData[i] = (short) (samples[i] * rescaleFactor);
Byte[] byteArr = new Byte[2];
byteArr = BitConverter.GetBytes(intData[i]);
byteArr.CopyTo(bytesData, i * 2);
}
fileStream.Write(bytesData, 0, bytesData.Length);
}
Answer by renezuidhof · Feb 08, 2015 at 01:43 AM
I think i have found the solution, for ppl getting this issue in the future.
The only thing you have to do is expand or shrink the data array you can get from an AudioClip (Interpolating in case of shrinking (pitch > 1), Extrapolating in case of expansion (Pitch < 1))
Google a bit for those kind of stuff and you should be able to figure it out :)
Answer by screenname_taken · Feb 06, 2015 at 01:48 PM
Dunno on saving the audio stream that's playing, but how about making a class that holds a sound file name and a pitch float, and the user saving that instead of recording and saving a whole audio file? Unless the whole point is for the user to export any funny clips they made.
Yea, i already do that :) but i also want ppl to save there audio to load later on. I've got this working by just storing the raw audio and storing a config file with the pitch.
But indeed, the next feature i want to add is to export it to other devices like pc's. So the audio itself needs to be changed.
I guess listening to the system's wave isn't possible. I mean Windows' "what you hear" path.
Your answer
Follow this Question
Related Questions
Slowly fade audio's pitch on key press? 3 Answers
Change Pitch on Collision 1 Answer
car shifting smoothed. help! 0 Answers
Rigidbody crate collide sound. 0 Answers
How to determine the pitch from GetSpectrumData() to make a polyphonic tuner? 1 Answer