Code Monkey home page Code Monkey logo

unitywav's Introduction

Wav Utility for Unity

WAV utility for recording and audio playback functions in Unity.

Usage

  • Use ToAudioClip method for loading wav file / bytes.
    • Loads .wav (PCM uncompressed) files at 8,16,24 and 32 bits and converts data to Unity's AudioClip.
    public AudioSource audioSource; // hook up with scene's AudioSource in Unity Editor inspector
    public void LoadWavFile (string filename)
      {
      	string path = string.Format ("{0}/{1}", Application.persistentDataPath, filename);
      	AudioClip audioClip = WavUtility.ToAudioClip (path);
      	audioSource.clip = audioClip;
      	audioSource.Play ();
      }
    
  • Use FromAudioClip method for saving wav file / bytes.
    • Converts an AudioClip's float data into wav byte array at 16 bit.
    private AudioClip audioClip;
    public int recordTime = 2; // no. of seconds can be set in Unity Editor inspector
    private const int sampleRate = 16000; // sample rate for recording speech
    public void RecordAudio ()
      {
      	if (Microphone.devices.Length == 0) {
      		Debug.LogWarning ("No microphone found to record audio clip sample with.");
      		return;
      	}
      	string mic = Microphone.devices [0];
      	audioClip = Microphone.Start (mic, false, recordTime, sampleRate);
      }
    
    public string SaveWavFile ()
      {
      	string filepath;
      	byte[] bytes = WavUtility.FromAudioClip (audioClip, out filepath, true);
      	return filepath;
      }
    

Notes

By design the ToAudioClip method only supports loading wav files that are stored using Unity's Application data path (Application.persistentDataPath or Application.dataPath). In order to load bundled resources it would be better to use Unity's Resources.Load("filename") typeof(AudioClip) method.

If you need to load WAV files from a remote URL then use the UnityWebRequest.GetAudioClip method which uses the AudioClip download handler.

public void LoadAudio ()
{
  string url = "<WAV_FILE_URL>";
  StartCoroutine (LoadAudioURL (url));
}
public IEnumerator LoadAudioURL (string url)
{
  UnityWebRequest www = UnityWebRequest.GetAudioClip (url, AudioType.WAV);
  yield return www.Send ();
  if (www.isError) {
    Debug.LogWarning ("Audio error:" + www.error);
  } else {
    AudioClip audioClip = ((DownloadHandlerAudioClip)www.downloadHandler).audioClip;
    audioSource.clip = audioClip;
    audioSource.Play ();
  }
}

Version history

  • 1.0 beta 1
    • Unity AudioClip to 16 bit wav file / bytes converter.
    • 8, 16, 24 or 32 bit PCM (uncompressed) wav file / bytes to Unity AudioClip converter.

Related info

Wav file formatting based on specs detailed in following articles:

Share the <3 @deadlyfingers

unitywav's People

Contributors

deadlyfingers avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.