Record sound: Difference between revisions
Content deleted Content added
m →{{header|Wren}}: Minor tidy |
Initial submission |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 19:
The programs in this page might use signed 16-bit or unsigned 16-bit samples, at 8000 Hz, 44100 Hz, or any other sample rate.
Therefore, these programs might not record sound in the same format.)
=={{header|Ada}}==
{{libheader|ASFML}}
<syntaxhighlight lang="ada">
with Sf.Audio.SoundBufferRecorder;
with Sf.Audio.SoundBuffer;
with Ada.Text_IO;
procedure Record_Sound is
use Sf, Sf.Audio, Ada.Text_IO;
Sound_Buffer_Recorder : constant sfSoundBufferRecorder_Ptr := SoundBufferRecorder.create;
Sound_Buffer : sfSoundBuffer_Ptr;
begin
if Sound_Buffer_Recorder = null then
Put_Line (Standard_Error, "Error: sound recorder not available!");
return;
end if;
-- By default the recording is in 16-bit mono. Using the
-- setChannelCount method you can change the number of channels
-- used by the audio capture device to record.
if SoundBufferRecorder.start (Sound_Buffer_Recorder, sampleRate => 44_100) /= sfTrue then
Put_Line (Standard_Error, "Error: sound recorder cannot start!");
return;
end if;
delay 10.0;
SoundBufferRecorder.stop (Sound_Buffer_Recorder);
Sound_Buffer := SoundBufferRecorder.getBuffer (Sound_Buffer_Recorder);
if SoundBuffer.saveToFile (Sound_Buffer, filename => "output.ogg") /= sfTrue then
Put_Line (Standard_Error, "Error: recorded sound could not be saved!");
end if;
SoundBufferRecorder.destroy (Sound_Buffer_Recorder);
end Record_Sound;
</syntaxhighlight>
=={{header|AutoHotkey}}==
Line 273 ⟶ 312:
(play (record 65536))
</syntaxhighlight>
=={{header|Diego}}==
This <code>funct</code>ion returns a <code>{wav}</code> variable recorded from a thing in the mist. It understands that the found <code>thing</code> has a microphone, so will have microphone knowledge. If the caller does not have microphone knowledge, the callee will train the caller on first request.
<syntaxhighlight lang="diego">begin_funct({wav}, Record sound);
set_decision(linger);
find_thing()_first()_microphone()_bitrate(16)_tech(PCM)_samplerate(signed16, unsigned16)_rangefrom(8000, Hz)_rangeto(44100, Hz)_export(.wav)
? with_found()_microphone()_label(mic);
: err_funct[]_err(Sorry, no one has a microphone!);
exit_funct[];
;
with_microphone[mic]_record()_durat({secs}, 30)_var(recording);
[Record sound]_ret([recording]);
reset_decision();
end_funct[];
// Record a monophonic 16-bit PCM sound into memory space:
exec_funct(Record sound)_var(PCMRecording)_me(); // The variable 'PCMRecording' is the sound in memory space
// Record a monophonic 16-bit PCM sound into a file or array:
exec_funct(Record sound)_file(foo.wav)_me(); // The file 'foo.wav' is the sound in a file</syntaxhighlight>
This is the <code>instruct</code>ion version, where the thing keeps the recording.
<syntaxhighlight lang="diego">begin_instruct(Record sound);
set_decision(linger);
find_thing()_first()_microphone()_bitrate(16)_tech(PCM)_samplerate(signed16, unsigned16)_rangefrom(8000, Hz)_rangeto(44100, Hz)_export(.wav)
? with_found()_label(recorder)_microphone()_label(mic);
: err_instruct[]_err(Sorry, no one has a microphone!);
exit_instruct[];
;
with_microphone[mic]_record()_durat({secs}, 30)_var(recording);
reset_decision();
end_instruct[];
// Record a monophonic 16-bit PCM sound into memory space:
exec_instruct(Record sound)_me();
with_thing[recorder]_microphone[mic]_var[recording]_var(PCMRecording); // The variable 'PCMRecording' is the sound in memory space
// Record a monophonic 16-bit PCM sound into a file or array:
exec_instruct(Record sound)_me();
with_thing[recorder]_microphone[mic]_var[recording]_file(foo.wav)_me(); // The file 'foo.wav' is the sound in a file</syntaxhighlight>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">#define PI 4 * Atn(1)
' Constants for the audio format
Dim Shared As Integer SAMPLE_RATE = 44100
Dim Shared As Integer BITS_PER_SAMPLE = 16
Dim Shared As Integer NUM_CHANNELS = 1
' Generates a sine wave
Sub generateSineWave(buffer() As Short, frequency As Double)
Dim As Double increment = 2.0 * PI * frequency / SAMPLE_RATE
Dim As Double x = 0.0
For i As Integer = 0 To Ubound(buffer)
buffer(i) = (Sin(x) * 32767.0)
x += increment
Next i
End Sub
' Write the header of the .wav file
Sub writeWaveHeader(file As Integer, numSamples As Integer)
' Write the RIFF header
Print #file, "RIFF";
Put #file, , numSamples * 2 + 36 ' File size
Print #file, "WAVE";
Dim As Integer SB = 16, FA = 1
' Write the fmt sub-block
Print #file, "fmt ";
Put #file, , SB ' Size of the fmt sub-block
Put #file, , FA ' Audio format (1 = PCM)
Put #file, , NUM_CHANNELS ' Number of channels
Put #file, , SAMPLE_RATE ' Sample rate
Put #file, , SAMPLE_RATE * NUM_CHANNELS * BITS_PER_SAMPLE / 8 ' Byte rate
Put #file, , NUM_CHANNELS * BITS_PER_SAMPLE / 8 ' Alineación de bloques
Put #file, , BITS_PER_SAMPLE ' Bits per sample
' Write the data sub-block
Print #file, "data";
Put #file, , numSamples * 2 ' Data size
End Sub
Dim As Integer file = Freefile
Open "output.wav" For Binary As #file
' Generates a 440 Hz sine wave for 5 seconds
Dim As Integer numSamples = SAMPLE_RATE * 5
Dim As Short buffer(numSamples - 1)
generateSineWave(buffer(), 440.0)
' Write the .wav file
writeWaveHeader(file, numSamples)
Put #file, , buffer()
Close #file</syntaxhighlight>
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
// ---------------
include "NSLog.incl"
include "Tlbx Speech.incl"
include "Tlbx AVFoundation.incl"
output "RecordToTextDemo"
#plist CFBundleIdentifier @"com.futurebasic.RecordToTextDemo"
#plist NSSpeechRecognitionUsageDescription @"Enable speech recognition."
#plist NSMicrophoneUsageDescription @"This app requires microphone access."
#define FILE_URL fn URLByAppendingPathComponent(fn FileManagerURLForApplicationDirectory,@"MyAudioFile.m4a")
#define RECORDER_KEY @"recorder"
_window = 1
begin enum 1
_recordBtn
end enum
void local fn FixViews
dispatchmain
AVAudioRecorderRef recorder = fn AppProperty( RECORDER_KEY )
if ( recorder )
button _recordBtn,,, @"Stop Recording",,,, _window
else
button _recordBtn,,, @"Start Recording",,,, _window
end if
dispatchend
end fn
void local fn BuildWindow
window _window, @"Record Demo", (0,0,480,270)
button _recordBtn,,, @"Start Recording", (13,13,129,32)
end fn
void local fn MyRecognitionTaskHandler( ref as SFSpeechRecognizerRef, result as SFSpeechRecognitionResultRef, err as ErrorRef, userData as ptr )
SFTranscriptionRef transcription = fn SFSpeechRecognitionResultBestTranscription( result )
if ( err )
NSLog(@"error")
else
if ( fn SFSpeechRecognitionResultIsFinal( result ) )
NSLog(@"%@",fn SFTranscriptionFormattedString( transcription ))
end if
end if
end fn
void local fn MyRequestAuthorization( status as SFSpeechRecognizerAuthorizationStatus, userData as ptr )
SFSpeechRecognizerRef recognizer
SFSpeechURLRecognitionRequestRef request
select ( status )
case SFSpeechRecognizerAuthorizationStatusNotDetermined : NSLog(@"Authorization not determined")
case SFSpeechRecognizerAuthorizationStatusDenied : NSLog(@"Authorization denied")
case SFSpeechRecognizerAuthorizationStatusRestricted : NSLog(@"Authorization restricted")
case SFSpeechRecognizerAuthorizationStatusAuthorized
recognizer = fn SFSpeechRecognizerInit
if ( fn SFSpeechRecognizerIsAvailable( recognizer ) )
if ( FILE_URL )
fn SoundPlay( fn SoundWithContentsOfURL( FILE_URL, YES ) )
request = fn SFSpeechURLRecognitionRequestWithURL( FILE_URL )
SFSpeechRecognizerSetSupportsOnDeviceRecognition( recognizer, YES )
fn SFSpeechRecognizerRecognitionTaskWithResultHandler( recognizer, request, @fn MyRecognitionTaskHandler, NULL )
end if
else
NSLog(@"Speech recognizer not available")
end if
end select
end fn
void local fn RecognizeSpeech
SFSpeechRecognizerRequestAuthorization( @fn MyRequestAuthorization, NULL )
end fn
void local fn RecordAudio
CFDictionaryRef settings = @{
AVFormatIDKey:@(kAudioFormatMPEG4AAC),
AVEncoderAudioQualityKey:@(AVAudioQualityHigh),
AVSampleRateKey:@44100.0,
AVNumberOfChannelsKey:@1,
AVLinearPCMBitDepthKey:@16}
AVAudioFormatRef format = fn AVAudioFormatWithSettings( settings )
AVAudioRecorderRef recorder = fn AVAudioRecorderWithFormat( FILE_URL, format, NULL )
AppSetProperty( RECORDER_KEY, recorder )
fn FixViews
fn AVAudioRecorderRecord( recorder )
end fn
void local fn MyRequestAccessHandler( granted as BOOL, userData as ptr )
if ( granted ) then fn RecordAudio
end fn
void local fn StartStopRecording
AVAudioRecorderRef recorder = fn AppProperty( RECORDER_KEY )
AVAuthorizationStatus status
if ( recorder )
AVAudioRecorderStop( recorder )
AppRemoveProperty( RECORDER_KEY )
fn FixViews
fn RecognizeSpeech
else
status = fn AVCaptureDeviceAuthorizationStatus( AVMediaTypeAudio )
select ( status )
case AVAuthorizationStatusDenied : NSLog(@"Denied")
case AVAuthorizationStatusRestricted : NSLog(@"Restricted")
case AVAuthorizationStatusAuthorized : fn RecordAudio
case AVAuthorizationStatusNotDetermined
AVCaptureDeviceRequestAccess( AVMediaTypeAudio, @fn MyRequestAccessHandler, NULL )
end select
end if
end fn
void local fn DoDialog( ev as long, tag as long )
select ( ev )
case _btnClick
select ( tag )
case _recordBtn : fn StartStopRecording
end select
end select
end fn
fn BuildWindow
on dialog fn DoDialog
HandleEvents
</syntaxhighlight>
=={{header|Go}}==
Line 341 ⟶ 619:
fmt.Println("Play-back completed.")
}</syntaxhighlight>
=={{header|GUISS}}==
|