@@ -48,62 +48,64 @@ public static void RunDemo(
48
48
int audioDeviceIndex ,
49
49
string outputPath = null )
50
50
{
51
- Porcupine porcupine = null ;
52
- BinaryWriter outputFileWriter = null ;
53
- int totalSamplesWritten = 0 ;
54
-
55
51
// init porcupine wake word engine
56
- porcupine = Porcupine . FromKeywordPaths ( accessKey , keywordPaths , modelPath , sensitivities ) ;
52
+ using Porcupine porcupine = Porcupine . FromKeywordPaths ( accessKey , keywordPaths , modelPath , sensitivities ) ;
57
53
58
54
// get keyword names for labeling detection results
59
55
List < string > keywordNames = keywordPaths . Select ( k => Path . GetFileNameWithoutExtension ( k ) . Split ( "_" ) [ 0 ] ) . ToList ( ) ;
60
56
57
+ // create recorder
58
+ using PvRecorder recorder = PvRecorder . Create ( frameLength : porcupine . FrameLength , deviceIndex : audioDeviceIndex ) ;
59
+ Console . WriteLine ( $ "Using device: { recorder . SelectedDevice } ") ;
60
+ Console . CancelKeyPress += delegate ( object sender , ConsoleCancelEventArgs e )
61
+ {
62
+ e . Cancel = true ;
63
+ recorder . Stop ( ) ;
64
+ Console . WriteLine ( "Stopping..." ) ;
65
+ } ;
66
+
61
67
// open stream to output file
68
+ BinaryWriter outputFileWriter = null ;
69
+ int totalSamplesWritten = 0 ;
62
70
if ( ! string . IsNullOrWhiteSpace ( outputPath ) )
63
71
{
64
72
outputFileWriter = new BinaryWriter ( new FileStream ( outputPath , FileMode . OpenOrCreate , FileAccess . Write ) ) ;
65
- WriteWavHeader ( outputFileWriter , 1 , 16 , 16000 , 0 ) ;
73
+ WriteWavHeader ( outputFileWriter , 1 , 16 , recorder . SampleRate , 0 ) ;
66
74
}
67
75
68
- Console . CancelKeyPress += ( s , o ) =>
69
- {
70
- Console . WriteLine ( "Stopping..." ) ;
71
-
72
- if ( outputFileWriter != null )
73
- {
74
- // write size to header and clean up
75
- WriteWavHeader ( outputFileWriter , 1 , 16 , 16000 , totalSamplesWritten ) ;
76
- outputFileWriter . Flush ( ) ;
77
- outputFileWriter . Dispose ( ) ;
78
- }
79
- porcupine ? . Dispose ( ) ;
80
- } ;
81
-
82
- // create and start recording
83
- using PvRecorder recorder = PvRecorder . Create ( deviceIndex : audioDeviceIndex , frameLength : porcupine . FrameLength ) ;
84
- Console . WriteLine ( $ "Using device: { recorder . SelectedDevice } ") ;
76
+ // start recording
85
77
Console . Write ( $ "Listening for [{ string . Join ( ' ' , keywordNames . Select ( k => $ "'{ k } '") ) } ]...\n ") ;
86
78
recorder . Start ( ) ;
87
79
88
- while ( true )
80
+ while ( recorder . IsRecording )
89
81
{
90
- short [ ] pcm = recorder . Read ( ) ;
82
+ short [ ] frame = recorder . Read ( ) ;
91
83
92
- int result = porcupine . Process ( pcm ) ;
84
+ int result = porcupine . Process ( frame ) ;
93
85
if ( result >= 0 )
94
86
{
95
87
Console . WriteLine ( $ "[{ DateTime . Now . ToLongTimeString ( ) } ] Detected '{ keywordNames [ result ] } '") ;
96
88
}
97
89
98
90
if ( outputFileWriter != null )
99
91
{
100
- foreach ( short sample in pcm )
92
+ foreach ( short sample in frame )
101
93
{
102
94
outputFileWriter . Write ( sample ) ;
103
95
}
104
- totalSamplesWritten += pcm . Length ;
96
+ totalSamplesWritten += frame . Length ;
105
97
}
106
- _ = Thread . Yield ( ) ;
98
+
99
+ Thread . Yield ( ) ;
100
+ }
101
+
102
+ if ( outputFileWriter != null )
103
+ {
104
+ // write size to header and clean up
105
+ WriteWavHeader ( outputFileWriter , 1 , 16 , recorder . SampleRate , totalSamplesWritten ) ;
106
+ outputFileWriter . Flush ( ) ;
107
+ outputFileWriter . Dispose ( ) ;
108
+ Console . Write ( $ "Wrote audio to '{ outputPath } '") ;
107
109
}
108
110
}
109
111
@@ -143,7 +145,7 @@ private static void WriteWavHeader(BinaryWriter writer, ushort channelCount, ush
143
145
/// </summary>
144
146
public static void ShowAudioDevices ( )
145
147
{
146
- string [ ] devices = PvRecorder . GetAudioDevices ( ) ;
148
+ string [ ] devices = PvRecorder . GetAvailableDevices ( ) ;
147
149
for ( int i = 0 ; i < devices . Length ; i ++ )
148
150
{
149
151
Console . WriteLine ( $ "index: { i } , device name: { devices [ i ] } ") ;
0 commit comments