Create an empty blank audio file of a specific duration in AudioKit - ios

In my project, I am using AudioKit to play beats. I want to create an empty audio file of the same length of the main beat. and and whatever starting point I choose within the duration of the main beat. it will start recording from that point and will end when the main beat is stopped. override the previously recorded part with the newly recorded file. can I achieve with this an empty audio file at the start. Please guide.

In a documentation you can find info about silent file: https://github.com/FT-BOYS/SoundMix/blob/master/AudioKit/Common/Internals/Audio%20File/AKAudioFile%2BUtilities.swift
To create file you can provide just the number of samples to generate (equals length in seconds multiplied by sample rate). It would look something like this:
let file = try! AKAudioFile.silent(samples: sampleRate * seconds)

Related

Google cloud speech very inaccurate and misses words on clean audio

I am using Google cloud speech through Python and finding many transcriptions are inaccurate and missing several words. This is a simple script I'm using to return a transcript of an audio file, in this case 'out307.wav':
client = speech.SpeechClient()
with io.open('out307.wav', 'rb') as audio_file:
content = audio_file.read()
audio = speech.types.RecognitionAudio(content=content)
config = speech.types.RecognitionConfig(
enable_word_time_offsets=True,
language_code='en-US',
audio_channel_count=1)
response = client.recognize(config, audio)
for result in response.results:
alternative = result.alternatives[0]
print(u'Transcript: {}'.format(alternative.transcript))
This returns the following transcript:
to do this the tensions and suspicions except
This is very far off what the actual audio says (I've uploaded it at https://vocaroo.com/i/s1zdZ0SOH1Ki). The audio is a .wav and very clear with no background noise. This is worse than average, as in some cases it will get the transcription fully correct on a 10 second audio file, or it may miss just a couple of words. Is there anything I can do to improve results?
This is weird, I tried your audio file with your code and I get the same result, but, if I change the language_code to "en-UK" I am able to get the full response.
I'm working for Google Cloud and I created for you a public issue here, you can track there the updates.

Creating a MIDI file from an AKKeyboardView

Currently I am using an AKKeyboardView to connect essentially to the AKRhodesPiano object, and I was wondering if there was an easy way to generate a MIDI file from this?
I see the AKKeyboardView has the noteOn and noteOff functions, which does produce the MIDINoteNumber but I can't find anywhere else in the AudioKit library to really take this input and generate a MIDI file, even if only just a simple one.
You would need to run an AKSequencer in the background (maybe with a metronome track). Make an additional track that you will record onto. Also set the length to be as long as you will need for the recording.
When you get a noteOn message from the keyboard, you can check the sequencer's currentPosition and record this into a dictionary. When you get the matching pitch's noteOff message, again check the currentPosition. Use the difference between these two times to get the duration and add a note to your recording track on the sequencer:
myRecordingTrack.add(noteNumber: noteNumber,
velocity: 127,
position: timeAtNoteOn,
duration: timeAtNoteOff - timeAtNoteOn,
channel: 0)
Then you could easily use AKSequencer's genData() to create a MIDI file (possibly either deleting the metronome track, or copying the recorded track to a new AKSequencer instance).
Check out the SequencerDemo for setting up AKSequencer and building sequences and MIDIFileEditAndSync (both in the iOS Example folder in the AudioKit repo) for an example of writing AKSequencer to a MIDI file.

Playing multi-sampled Instruments using AudioKit, controlling ADSR envelope

I'm trying to play instrument of several .wav samples using AudioKit.
I've tried so far:
Using AKSampler (with underlying AVAudioUnitSampler) – it worked fine, but I can't figure out how to control ADSR envelope here – calling stop will stop note immediately.
Another way is to use AKSamplePlayer for each sample and play it, manually setting rate so it play the right note. I can (possibly?) then connect AKAmplitudeEnvelope to each sample player. But if I want to play 5 notes of the same sample simultaneously, I would need 5 instances of AKSamplePlayer, which seems like wasting resources.
I also tried to find a way to just push raw audio samples to the AudioKit output buffer, making mixing and sample interpolation by myself (in C, probably?). But didn't find how to do it :(
What is the right way to make a multi-sampled instrument using AudioKit? I feel like it must be a fairly simple task.
Thanks to mahal tertin, it's pretty easy to use AKAUPresetBuilder!
You can create .aupreset file somewhere in tmp directory and then load this instrument with AKSampler.
The only thing worth noting is that by default AKAUPresetBuilder will generate samples with trigger mode set to trigger, which will ignore note-off events. So you should set it explicitly.
For example:
let sampleC4 = AKAUPresetBuilder.generateDictionary(
rootNote: 60,
filename: pathToC4WavSample,
startNote: 48,
endNote: 65)
sampleC4["triggerMode"] = "hold"
let sampleC5 = AKAUPresetBuilder.generateDictionary(
rootNote: 72,
filename: pathToC5WavSample,
startNote: 66,
endNote: 83)
sampleC5["triggerMode"] = "hold"
AKAUPresetBuilder.createAUPreset(
dict: [sampleC4, sampleC5],
path: pathToAUPresetFilename,
instrumentName: "My Instrument",
attack: 0,
release: 0.2)
and then create a sampler and start AudioKit:
sampler = AKSampler()
try sampler.loadInstrument(atPath: pathToAUPresetFilename)
AudioKit.output = sampler
AudioKit.start()
and then use this to start playing note:
sampler.play(noteNumber: MIDINoteNumber(63), velocity: MIDIVelocity(120), channel: 0)
and this to stop, respecting release parameter:
sampler.stop(noteNumber: MIDINoteNumber(63), channel: 0)
Probably the best way would be to embed your wav files into an EXS or Soundfont format, making use of tools in that realm to accomplish the ADSR for instance. Otherwise you'll kind of have to have an instrument for each sample.

Why I am receiving only a few audio samples per second when using AVAssetReader on iOS?

I'm coding something that:
record video+audio with the built-in camera and mic (AVCaptureSession),
do some stuff with the video and audio samplebuffer in realtime,
save the result into a local .mp4 file using AVAssetWritter,
then (later) read the file (video+audio) using AVAssetReader,
do some other stuff with the samplebuffer (for now I do nothing),
and write the result into a final video file using AVAssetWriter.
Everything works well but I have an issue with the audio format:
When I capture the audio samples from the capture session, I can log about 44 samples/sec, which seams to be normal.
When I read the .mp4 file, I only log about 3-5 audio samples/sec!
But the 2 files look and sound exactly the same (in QuickTime).
I didn't set any audio settings for the Capture Session (as Apple doesn't allow it).
I configured the outputSettings of the 2 audio AVAssetWriterInput as follow:
NSDictionary *settings = #{
AVFormatIDKey:#(kAudioFormatLinearPCM),
AVNumberOfChannelsKey:#(2),
AVSampleRateKey:#(44100.),
AVLinearPCMBitDepthKey:#(16),
AVLinearPCMIsNonInterleaved:#(NO),
AVLinearPCMIsFloatKey:#(NO),
AVLinearPCMIsBigEndianKey:#(NO)
};
I pass nil to the outputSettings of the audio AVAssetReaderTrackOutput in order to receive samples as stored in the track (according to the doc).
So, the sample rate should be 44100Hz from the CaptureSession to the final file. Why I am reading only a few audio samples? And why is it working anyway? I have the intuition that it will not work well when I'll have to work with the samples (I need to update their timestamps for example).
I tried several other settings (such as kAudioFormatMPEG4AAC), but AVAssetReader can't read compressed audio formats.
Thanks for your help :)

Not able to play the .wav sound which i recorded through coding in blackberry

i have tried to record the audio in wav format and i succeeded but when i try to play that file, it gives an error that 'media file is of unsupported format'.
the main portion of code is as follows:
player = Manager.createPlayer("capture://audio?encoding=pcm&rate=44100&bits=16&channels=1");
player.realize();
controller = (RecordControl) player.getControl("RecordControl");
controller.setRecordLocation("file:///SDCard/BlackBerry/voicenotes/voice.wav");
//controller.setRecordSizeLimit(396900);
controller.startRecord();
player.start();
Thread.sleep(7000);
controller.commit();
player.close();
This code works well and it gave me a voice.wav file but i am not able to play this file.Is there is something which i forget?
Thanks in advance.
Sounds like the same problem as here:
http://supportforums.blackberry.com/t5/Java-Development/Bug-in-media-Manager-Player/td-p/1009027/page/2
Essentially, you need to add the RIFF/WAVE-header for the recorded data yourself, see the last post on the thread for example.

Resources