Skip to content

Commit

Permalink
Added timeout flag, man page HTML generation + more, bumped version t…
Browse files Browse the repository at this point in the history
…o 0.5
  • Loading branch information
sveinbjornt committed Nov 4, 2023
1 parent 965af5e commit 3166e22
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 23 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
XCODE_PROJ := "hear.xcodeproj"
PROGRAM_NAME := "hear"
BUILD_DIR := "products"
VERSION := "0.4"
VERSION := "0.5"

all: clean build_unsigned

release: clean build_signed archive size
release: clean build_signed archive man size

test: clean build_unsigned runtests

Expand Down Expand Up @@ -47,6 +47,9 @@ size:
@echo "Archive size:"
@cd "$(BUILD_DIR)"; du -hs "$(PROGRAM_NAME)-$(VERSION).zip"

man:
/usr/bin/man ./hear.1 | ./cat2html > hear.1.html

runtests:
# The tests don't work in CI env due to missing permissions from macOS
# @echo "Running tests"
Expand Down
Binary file added cat2html
Binary file not shown.
6 changes: 4 additions & 2 deletions hear.1
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ input device, e.g. microphone).
.It Fl p -punctuation
Add punctuation to speech recognition results.
.It Fl x -exit Ar exit_word
Set exit word. This causes hear to exit when a speech recognition result ends
with the word in question.
Set exit word. This causes the program to exit when a speech recognition result
ends with the specified word.
.It Fl t -timeout Ar seconds
Exit if no recognition results are received within the specified number of seconds.
.It Fl h -help
Print help and exit.
.It Fl v -version
Expand Down
11 changes: 7 additions & 4 deletions hear.1.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<b>SYNOPSIS</b>

<b>hear</b> [<b>-hvdmps</b>] [<b>-i</b> <u>audio</u><b>_<u></b>file</u>] [<b>-l</b> <u>language</u>] [<b>-x</b> <u>exit</u><b>_<u></b>word</u>]
<b>hear</b> [<b>-hvdmps</b>] [<b>-i</b> <u>audio</u><b>_<u></b>file</u>] [<b>-l</b> <u>locale</u>] [<b>-x</b> <u>exit</u><b>_<u></b>word</u>]

<b>DESCRIPTION</b>

Expand All @@ -19,10 +19,9 @@
The following flags are supported:

<b>-s</b> <b>--supported</b>
Print list of supported languages (locales) for speech
recognition.
Print list of supported locales for speech recognition and exit.

<b>-l</b> <b>--language</b> <u>lang</u>
<b>-l</b> <b>--locale</b> <u>loc</u>
Specify speech recognition language (locale). Default is 'en-
US'.

Expand All @@ -49,6 +48,10 @@
Set exit word. This causes hear to exit when a speech
recognition result ends with the word in question.

<b>-t</b> <b>--timeout</b> <u>seconds</u>
Exit if no recognition results are received within the specified
number of seconds.

<b>-h</b> <b>--help</b>
Print help and exit.

Expand Down
4 changes: 2 additions & 2 deletions hear.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Hear < Formula
desc "Command-line speech recognition and transcription for macOS"
homepage "https://sveinbjorn.org/hear"
url "https://github.com/sveinbjornt/hear/archive/refs/tags/0.3.tar.gz"
url "https://github.com/sveinbjornt/hear/archive/refs/tags/0.5.tar.gz"
sha256 "671982c2361e636c79c5118684f73df2d0f282461e128ea0dc6034165819c520"
license "BSD-3-Clause"

depends_on xcode: ["9.3", :build]
depends_on xcode: ["10.0", :build]
depends_on macos: :catalina

def install
Expand Down
16 changes: 11 additions & 5 deletions hear.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
F45C87A527C0457A00EF9845 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
F47FA1B7290B31C500914395 /* TODO.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = TODO.md; sourceTree = "<group>"; };
F47FA1B8290B38EF00914395 /* test.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = test.mp3; sourceTree = "<group>"; };
F4A4D9472AF544D20036590C /* hear.1.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = hear.1.html; sourceTree = "<group>"; };
F4A4D9482AF5455F0036590C /* upload_manpage.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = upload_manpage.sh; sourceTree = "<group>"; };
F4A6209E29EEC7BB00DD9BE1 /* Speech.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Speech.framework; path = System/Library/Frameworks/Speech.framework; sourceTree = SDKROOT; };
F4A620A029F2D79800DD9BE1 /* hear.rb */ = {isa = PBXFileReference; lastKnownFileType = text.script.ruby; path = hear.rb; sourceTree = "<group>"; };
F4ACB73928DB41DA00E9E778 /* hear.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = hear.entitlements; sourceTree = "<group>"; };
Expand Down Expand Up @@ -82,11 +84,13 @@
F45C87A027C0457900EF9845 /* Makefile */,
F439F2C027D5157A00626667 /* install.sh */,
F45C879D27C0457900EF9845 /* hear.1 */,
F4A4D9472AF544D20036590C /* hear.1.html */,
F4A4D9482AF5455F0036590C /* upload_manpage.sh */,
F45C87A527C0457A00EF9845 /* Info.plist */,
F45C87AA27C0458300EF9845 /* src */,
F439F2C227D51D7F00626667 /* test */,
F43A461C27C010B2000469BC /* Products */,
F4A6209D29EEC7BB00DD9BE1 /* Frameworks */,
F43A461C27C010B2000469BC /* Products */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -146,7 +150,7 @@
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = 1;
LastUpgradeCheck = 1400;
LastUpgradeCheck = 1500;
TargetAttributes = {
F43A461A27C010B2000469BC = {
CreatedOnToolsVersion = 13.2.1;
Expand Down Expand Up @@ -244,7 +248,8 @@
DEBUG_INFORMATION_FORMAT = dwarf;
DEPLOYMENT_POSTPROCESSING = YES;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = NO;
ENABLE_TESTABILITY = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -306,6 +311,7 @@
DEPLOYMENT_POSTPROCESSING = YES;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand Down Expand Up @@ -335,7 +341,7 @@
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = Info.plist;
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 0.4;
MARKETING_VERSION = 0.5;
PRODUCT_BUNDLE_IDENTIFIER = org.sveinbjorn.hear;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand All @@ -356,7 +362,7 @@
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = Info.plist;
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 0.4;
MARKETING_VERSION = 0.5;
PRODUCT_BUNDLE_IDENTIFIER = org.sveinbjorn.hear;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand Down
2 changes: 1 addition & 1 deletion src/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#import <Foundation/Foundation.h>

#define PROGRAM_NAME @"hear"
#define PROGRAM_VERSION @"0.4"
#define PROGRAM_VERSION @"0.5"
#define PROGRAM_AUTHOR @"Sveinbjorn Thordarson"
#define PROGRAM_AUTHOR_EMAIL @"[email protected]"

Expand Down
3 changes: 2 additions & 1 deletion src/Hear.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
onDevice:(BOOL)useOnDeviceRecognition
singleLineMode:(BOOL)singleLine
addPunctuation:(BOOL)addPunctuation
exitWord:(NSString *)exitWord;
exitWord:(NSString *)exitWord
timeout:(CGFloat)timeout;

@end
40 changes: 38 additions & 2 deletions src/Hear.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ @interface Hear()

@property (nonatomic, retain) AVAudioEngine *engine;

@property (nonatomic, retain) NSTimer *timeoutTimer;

@property (nonatomic, retain) NSString *locale;
@property (nonatomic, retain) NSString *inputFile;
@property (nonatomic) BOOL useDeviceInput;
@property (nonatomic) BOOL useOnDeviceRecognition;
@property (nonatomic) BOOL singleLineMode;
@property (nonatomic) BOOL addPunctuation;
@property (nonatomic, retain) NSString *exitWord;
@property (nonatomic) CGFloat timeout;

@end

Expand All @@ -58,7 +61,8 @@ - (instancetype)initWithLocale:(NSString *)loc
onDevice:(BOOL)onDevice
singleLineMode:(BOOL)singleLine
addPunctuation:(BOOL)punctuation
exitWord:(NSString *)exitWord {
exitWord:(NSString *)exitWord
timeout:(CGFloat)timeout {
self = [super init];
if (self) {

Expand All @@ -74,6 +78,7 @@ - (instancetype)initWithLocale:(NSString *)loc
self.useDeviceInput = (input == nil);
self.addPunctuation = punctuation;
self.exitWord = exitWord;
self.timeout = timeout;
}
return self;
}
Expand Down Expand Up @@ -235,6 +240,10 @@ - (void)startListening {
return;
}

if (self.timeout > 0) {
[self startTimer:self];
}

// Print to stdout
NSString *transcript = result.bestTranscription.formattedString;
if (self.singleLineMode) {
Expand Down Expand Up @@ -278,13 +287,40 @@ - (void)startListening {
^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
[(SFSpeechAudioBufferRecognitionRequest *)self.request appendAudioPCMBuffer:buffer];
}];

// Start engine
NSError *err;
[self.engine startAndReturnError:&err];
if (err != nil) {
[self die:[NSString stringWithFormat:@"Failed to start audio engine: %@", [err localizedDescription]]];
}

if (self.timeout > 0) {
[self startTimer:self];
}
}

- (void)startTimer:(id)sender {
[self performSelectorOnMainThread:@selector(_startTimer:)
withObject:self
waitUntilDone:NO];
}

- (void)_startTimer:(id)sender {
if (self.timeoutTimer != nil) {
[self.timeoutTimer invalidate];
}
self.timeoutTimer = [NSTimer timerWithTimeInterval:self.timeout
target:self
selector:@selector(timedOut:)
userInfo:self
repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:self.timeoutTimer
forMode:NSDefaultRunLoopMode];
}

- (void)timedOut:(id)sender {
exit(EXIT_SUCCESS);
}

#pragma mark - Class methods
Expand Down
17 changes: 13 additions & 4 deletions src/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
static inline void PrintHelp(void);

// Command line options
static const char optstring[] = "sl:i:dpmx:hv";
static const char optstring[] = "sl:i:dpmx:t:hv";

static struct option long_options[] = {
// List supported locales for speech to text
Expand All @@ -60,6 +60,8 @@
{"mode", no_argument, 0, 'm'},
// Exit word
{"exit-word", required_argument, 0, 'x'},
// Timeout (in seconds)
{"timeout", required_argument, 0, 'x'},
// Print help
{"help", no_argument, 0, 'h'},
// Print version
Expand All @@ -82,6 +84,7 @@ int main(int argc, const char * argv[]) { @autoreleasepool {
BOOL useOnDeviceRecognition = NO;
BOOL singleLineMode = NO;
BOOL addsPunctuation = NO;
CGFloat timeout = 0.0f;

// Parse arguments
int optch;
Expand Down Expand Up @@ -127,6 +130,10 @@ int main(int argc, const char * argv[]) { @autoreleasepool {
exitWord = @(optarg);
break;

case 't':
timeout = [@(optarg) floatValue];
break;

// Print version
case 'v':
PrintVersion();
Expand All @@ -148,9 +155,10 @@ int main(int argc, const char * argv[]) { @autoreleasepool {
onDevice:useOnDeviceRecognition
singleLineMode:singleLineMode
addPunctuation:addsPunctuation
exitWord:exitWord];
exitWord:exitWord
timeout:timeout];
[[NSApplication sharedApplication] setDelegate:hear];
[[NSApplication sharedApplication] run];
[NSApp run];

return EXIT_SUCCESS;
}}
Expand All @@ -173,7 +181,7 @@ static inline void PrintVersion(void) {
static inline void PrintHelp(void) {
PrintVersion();
NSPrint(@"\n\
%@ [-vhmsdp] [-l lang] [-i file] [-x word]\n\
%@ [-vhmsdp] [-l lang] [-i file] [-x word] [-t seconds]\n\
\n\
Options:\n\
\n\
Expand All @@ -185,6 +193,7 @@ static inline void PrintHelp(void) {
-m --mode Enable single-line output mode (mic only)\n\
-p --punctuation Add punctuation to speech recognition results (macOS 13+)\n\
-x --exit-word Set exit word that causes program to quit\n\
-t --timeout Set silence timeout (in seconds)\n\
\n\
-h --help Prints help\n\
-v --version Prints program name and version\n\
Expand Down
5 changes: 5 additions & 0 deletions upload_manpage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

USER=$1

scp hear.1.html $USER@sveinbjorn.org:/www/sveinbjorn/html/files/manpages/hear.1.html

0 comments on commit 3166e22

Please sign in to comment.