From a11adeea4ad1fe38cd2f267ca62de32ce9c5d629 Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Wed, 3 May 2017 13:06:10 +0200 Subject: [PATCH 1/5] Fix luasocket compatibility with luajit 2.1.0 beta 3 (fix #1277) Note that luasocket 3.0 (in minor) targets lua 5.1, so it does not rely on a 5.0 compatibility feature, and does not require this patch. Since a 0.10.3 release seems unlikely at this point, this commit is mostly targeted at future packagers of older love versions. --- changes.txt | 1 + src/libraries/luasocket/libluasocket/lua.h | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/changes.txt b/changes.txt index 849ef40b8..66c6b9a35 100644 --- a/changes.txt +++ b/changes.txt @@ -8,6 +8,7 @@ Released: N/A * Fixed Shader:send and Shader:sendColor ignoring the last argument for an array. * Fixed a crash when love.graphics.pop is called after a love.window.setMode while the transformation stack was not empty. * Fixed BezierCurves to error instead of hanging in some situations. + * Fixed compilation of luasocket with newer luajit 2.1.0 beta versions. * Improved command line argument handling. * Improved seeking support, especially for short video files. diff --git a/src/libraries/luasocket/libluasocket/lua.h b/src/libraries/luasocket/libluasocket/lua.h index a280df842..c4215ecb7 100644 --- a/src/libraries/luasocket/libluasocket/lua.h +++ b/src/libraries/luasocket/libluasocket/lua.h @@ -6,9 +6,12 @@ #include #include -#if LUA_VERSION_NUM > 501 +#if LUA_VERSION_NUM >= 501 +# undef luaL_reg # define luaL_reg luaL_Reg +# undef luaL_putchar # define luaL_putchar(B, c) luaL_addchar(B, c) +# undef luaL_typerror # define luaL_typerror(L, n, t) luax_typerror(L, n, t) extern int luax_typerror(lua_State *L, int narg, const char *type); From 4c145e8c130c68662c6f35d55953e582dcf898c8 Mon Sep 17 00:00:00 2001 From: Tiago Koji Castro Shibata Date: Fri, 2 Jun 2017 19:35:58 -0300 Subject: [PATCH 2/5] Support audio mix modes --- .../xcode/liblove.xcodeproj/project.pbxproj | 4 +++ src/common/ios.h | 10 ++++++ src/common/ios.mm | 34 +++++++++++++++++++ src/modules/audio/wrap_Audio.cpp | 12 +++++++ src/modules/love/love.cpp | 14 ++++++++ src/scripts/boot.lua | 8 +++++ src/scripts/boot.lua.h | 12 +++++++ 7 files changed, 94 insertions(+) diff --git a/platform/xcode/liblove.xcodeproj/project.pbxproj b/platform/xcode/liblove.xcodeproj/project.pbxproj index d21bd683c..f958de2d3 100644 --- a/platform/xcode/liblove.xcodeproj/project.pbxproj +++ b/platform/xcode/liblove.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + CEEB36EA1EE21DB90085BD77 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CEEB36E91EE21DB90085BD77 /* AVFoundation.framework */; }; FA0B791B1A958E3B000E1D17 /* b64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B78F71A958E3B000E1D17 /* b64.cpp */; }; FA0B791C1A958E3B000E1D17 /* b64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B78F71A958E3B000E1D17 /* b64.cpp */; }; FA0B791D1A958E3B000E1D17 /* b64.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0B78F81A958E3B000E1D17 /* b64.h */; }; @@ -918,6 +919,7 @@ /* Begin PBXFileReference section */ 503971A86B7167A91B670FBA /* boot.lua.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = boot.lua.h; sourceTree = ""; }; + CEEB36E91EE21DB90085BD77 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; }; FA08F5AE16C7525600F007B5 /* liblove-macosx.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "liblove-macosx.plist"; path = "macosx/liblove-macosx.plist"; sourceTree = ""; }; FA0B78DD1A958B90000E1D17 /* liblove.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = liblove.a; sourceTree = BUILT_PRODUCTS_DIR; }; FA0B78F71A958E3B000E1D17 /* b64.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = b64.cpp; sourceTree = ""; }; @@ -1597,6 +1599,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + CEEB36EA1EE21DB90085BD77 /* AVFoundation.framework in Frameworks */, FA27B3C91B498623008A9DCE /* Theora.framework in Frameworks */, FA317EBA18F28B6D00B0BCD7 /* libz.dylib in Frameworks */, FAA627CE18E7E1560080752D /* CoreServices.framework in Frameworks */, @@ -2824,6 +2827,7 @@ FA577A6616C7199700860150 /* Frameworks */ = { isa = PBXGroup; children = ( + CEEB36E91EE21DB90085BD77 /* AVFoundation.framework */, FA0B7EEB1A959125000E1D17 /* macosx */, ); name = Frameworks; diff --git a/src/common/ios.h b/src/common/ios.h index 43a4c7c7c..1b74a6a1e 100644 --- a/src/common/ios.h +++ b/src/common/ios.h @@ -64,6 +64,16 @@ std::string getExecutablePath(); **/ void vibrate(); +/** + * Enable mix mode (e.g. with background music apps) and playback with a muted device. + **/ +void setAudioMixWithOthers(bool mixMode, bool playMuted); + +/** + * Returns whether another application is playing audio. + **/ +bool audioShouldBeSilenced(); + } // ios } // love diff --git a/src/common/ios.mm b/src/common/ios.mm index c17c17bde..70a7f35ce 100644 --- a/src/common/ios.mm +++ b/src/common/ios.mm @@ -26,6 +26,7 @@ #import #import +#import #include @@ -344,6 +345,39 @@ void vibrate() } } +void setAudioMixWithOthers(bool mixMode, bool playMuted) +{ + @autoreleasepool + { + NSError* err; + if (mixMode) + { + if (playMuted) + { + [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&err]; + } + else + { + [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&err]; + } + } + else + { + if (playMuted) + { + [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&err]; + } + } + if (err != nil) + NSLog(@"Error in AVAudioSession setCategory: %@", [err localizedDescription]); + } +} + +bool audioShouldBeSilenced() +{ + return [[AVAudioSession sharedInstance] secondaryAudioShouldBeSilencedHint]; +} + } // ios } // love diff --git a/src/modules/audio/wrap_Audio.cpp b/src/modules/audio/wrap_Audio.cpp index 0e617ff76..956f8da23 100644 --- a/src/modules/audio/wrap_Audio.cpp +++ b/src/modules/audio/wrap_Audio.cpp @@ -25,6 +25,7 @@ #include "null/Audio.h" #include "common/runtime.h" +#include "common/ios.h" // C++ #include @@ -293,6 +294,16 @@ int w_getDistanceModel(lua_State *L) return 1; } +int w_getShouldBeSilenced(lua_State *L) +{ +#ifdef LOVE_IOS + lua_pushboolean(L, love::ios::audioShouldBeSilenced()); +#else + lua_pushboolean(L, 0); +#endif + return 1; +} + // List of functions to wrap. static const luaL_Reg functions[] = { @@ -318,6 +329,7 @@ static const luaL_Reg functions[] = { "stopRecording", w_stopRecording },*/ { "setDistanceModel", w_setDistanceModel }, { "getDistanceModel", w_getDistanceModel }, + { "getShouldBeSilenced", w_getShouldBeSilenced }, { 0, 0 } }; diff --git a/src/modules/love/love.cpp b/src/modules/love/love.cpp index 64d3d4586..f56bd7eee 100644 --- a/src/modules/love/love.cpp +++ b/src/modules/love/love.cpp @@ -23,6 +23,7 @@ #include "common/version.h" #include "common/runtime.h" #include "common/wrap_Data.h" +#include "common/ios.h" #include "love.h" @@ -258,6 +259,14 @@ static int w__setGammaCorrect(lua_State *L) return 0; } +#ifdef LOVE_IOS +static int w__setAudioMixMode(lua_State *L) +{ + love::ios::setAudioMixWithOthers(lua_toboolean(L, 1), lua_toboolean(L, 2)); + return 0; +} +#endif + int luaopen_love(lua_State *L) { love::luax_insistpinnedthread(L); @@ -291,6 +300,11 @@ int luaopen_love(lua_State *L) lua_pushcfunction(L, w__setGammaCorrect); lua_setfield(L, -2, "_setGammaCorrect"); +#ifdef LOVE_IOS + lua_pushcfunction(L, w__setAudioMixMode); + lua_setfield(L, -2, "_setAudioMixMode"); +#endif + lua_newtable(L); for (int i = 0; love::VERSION_COMPATIBILITY[i] != nullptr; i++) diff --git a/src/scripts/boot.lua b/src/scripts/boot.lua index ed903a61a..418c557d3 100644 --- a/src/scripts/boot.lua +++ b/src/scripts/boot.lua @@ -373,6 +373,10 @@ function love.init() window = true, video = true, }, + audio = { + mixmode = true, + playmuted = false, + }, console = false, -- Only relevant for windows. identity = false, appendidentity = false, @@ -416,6 +420,10 @@ function love.init() love._setGammaCorrect(c.gammacorrect) end + if love._setAudioMixMode then + love._setAudioMixMode(c.audio.mixmode, c.audio.playmuted) + end + -- Gets desired modules. for k,v in ipairs{ "thread", diff --git a/src/scripts/boot.lua.h b/src/scripts/boot.lua.h index 6d0fbadb7..e53ba39ab 100644 --- a/src/scripts/boot.lua.h +++ b/src/scripts/boot.lua.h @@ -682,6 +682,11 @@ const unsigned char boot_lua[] = 0x09, 0x09, 0x09, 0x77, 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a, 0x09, 0x09, 0x09, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a, 0x09, 0x09, 0x7d, 0x2c, 0x0a, + 0x09, 0x09, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x20, 0x3d, 0x20, 0x7b, 0x0a, + 0x09, 0x09, 0x09, 0x6d, 0x69, 0x78, 0x6d, 0x6f, 0x64, 0x65, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a, + 0x09, 0x09, 0x09, 0x70, 0x6c, 0x61, 0x79, 0x6d, 0x75, 0x74, 0x65, 0x64, 0x20, 0x3d, 0x20, 0x66, 0x61, 0x6c, + 0x73, 0x65, 0x2c, 0x0a, + 0x09, 0x09, 0x7d, 0x2c, 0x0a, 0x09, 0x09, 0x63, 0x6f, 0x6e, 0x73, 0x6f, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x2c, 0x20, 0x2d, 0x2d, 0x20, 0x4f, 0x6e, 0x6c, 0x79, 0x20, 0x72, 0x65, 0x6c, 0x65, 0x76, 0x61, 0x6e, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x77, 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x73, 0x2e, 0x0a, @@ -776,6 +781,13 @@ const unsigned char boot_lua[] = 0x72, 0x72, 0x65, 0x63, 0x74, 0x28, 0x63, 0x2e, 0x67, 0x61, 0x6d, 0x6d, 0x61, 0x63, 0x6f, 0x72, 0x72, 0x65, 0x63, 0x74, 0x29, 0x0a, 0x09, 0x65, 0x6e, 0x64, 0x0a, + 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x5f, 0x73, 0x65, 0x74, 0x41, 0x75, 0x64, 0x69, 0x6f, + 0x4d, 0x69, 0x78, 0x4d, 0x6f, 0x64, 0x65, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, + 0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x5f, 0x73, 0x65, 0x74, 0x41, 0x75, 0x64, 0x69, 0x6f, 0x4d, 0x69, + 0x78, 0x4d, 0x6f, 0x64, 0x65, 0x28, 0x63, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2e, 0x6d, 0x69, 0x78, 0x6d, + 0x6f, 0x64, 0x65, 0x2c, 0x20, 0x63, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2e, 0x70, 0x6c, 0x61, 0x79, 0x6d, + 0x75, 0x74, 0x65, 0x64, 0x29, 0x0a, + 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x2d, 0x2d, 0x20, 0x47, 0x65, 0x74, 0x73, 0x20, 0x64, 0x65, 0x73, 0x69, 0x72, 0x65, 0x64, 0x20, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x73, 0x2e, 0x0a, 0x09, 0x66, 0x6f, 0x72, 0x20, 0x6b, 0x2c, 0x76, 0x20, 0x69, 0x6e, 0x20, 0x69, 0x70, 0x61, 0x69, 0x72, 0x73, From 74c35358bb849d529fdb910eef02f3acc2fcba9b Mon Sep 17 00:00:00 2001 From: Tiago Koji Castro Shibata Date: Sat, 10 Jun 2017 00:07:39 -0300 Subject: [PATCH 3/5] Move setMixMode to love.audio --- .../xcode/liblove.xcodeproj/project.pbxproj | 4 ---- platform/xcode/love.xcodeproj/project.pbxproj | 4 ++++ src/common/ios.mm | 22 +++++++++---------- src/modules/audio/wrap_Audio.cpp | 14 ++++++++++++ src/modules/love/love.cpp | 14 ------------ src/scripts/boot.lua | 8 +++---- src/scripts/boot.lua.h | 14 ++++++------ 7 files changed, 39 insertions(+), 41 deletions(-) diff --git a/platform/xcode/liblove.xcodeproj/project.pbxproj b/platform/xcode/liblove.xcodeproj/project.pbxproj index f958de2d3..d21bd683c 100644 --- a/platform/xcode/liblove.xcodeproj/project.pbxproj +++ b/platform/xcode/liblove.xcodeproj/project.pbxproj @@ -7,7 +7,6 @@ objects = { /* Begin PBXBuildFile section */ - CEEB36EA1EE21DB90085BD77 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CEEB36E91EE21DB90085BD77 /* AVFoundation.framework */; }; FA0B791B1A958E3B000E1D17 /* b64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B78F71A958E3B000E1D17 /* b64.cpp */; }; FA0B791C1A958E3B000E1D17 /* b64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B78F71A958E3B000E1D17 /* b64.cpp */; }; FA0B791D1A958E3B000E1D17 /* b64.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0B78F81A958E3B000E1D17 /* b64.h */; }; @@ -919,7 +918,6 @@ /* Begin PBXFileReference section */ 503971A86B7167A91B670FBA /* boot.lua.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = boot.lua.h; sourceTree = ""; }; - CEEB36E91EE21DB90085BD77 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; }; FA08F5AE16C7525600F007B5 /* liblove-macosx.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "liblove-macosx.plist"; path = "macosx/liblove-macosx.plist"; sourceTree = ""; }; FA0B78DD1A958B90000E1D17 /* liblove.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = liblove.a; sourceTree = BUILT_PRODUCTS_DIR; }; FA0B78F71A958E3B000E1D17 /* b64.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = b64.cpp; sourceTree = ""; }; @@ -1599,7 +1597,6 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - CEEB36EA1EE21DB90085BD77 /* AVFoundation.framework in Frameworks */, FA27B3C91B498623008A9DCE /* Theora.framework in Frameworks */, FA317EBA18F28B6D00B0BCD7 /* libz.dylib in Frameworks */, FAA627CE18E7E1560080752D /* CoreServices.framework in Frameworks */, @@ -2827,7 +2824,6 @@ FA577A6616C7199700860150 /* Frameworks */ = { isa = PBXGroup; children = ( - CEEB36E91EE21DB90085BD77 /* AVFoundation.framework */, FA0B7EEB1A959125000E1D17 /* macosx */, ); name = Frameworks; diff --git a/platform/xcode/love.xcodeproj/project.pbxproj b/platform/xcode/love.xcodeproj/project.pbxproj index 3c46db730..4a0425ef5 100644 --- a/platform/xcode/love.xcodeproj/project.pbxproj +++ b/platform/xcode/love.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ A9D307F2106635D3004FEDF8 /* physfs.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = A9D307E9106635C3004FEDF8 /* physfs.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; }; A9F169AC109E825000FC83D1 /* mpg123.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = A9F169A6109E824900FC83D1 /* mpg123.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; }; A9F169AD109E825000FC83D1 /* libmodplug.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = A9F16926109E7BAD00FC83D1 /* libmodplug.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; }; + CE73F8001EEB64150052DAB3 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE73F7FF1EEB64150052DAB3 /* AVFoundation.framework */; }; FA0797991BF480A200034B7C /* GameController.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA0797981BF480A200034B7C /* GameController.framework */; settings = {ATTRIBUTES = (Weak, ); }; }; FA08F69616C766E000F007B5 /* love.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA08F69116C765A200F007B5 /* love.framework */; }; FA08F69716C766E700F007B5 /* love.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = FA08F69116C765A200F007B5 /* love.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; }; @@ -110,6 +111,7 @@ A9D307E9106635C3004FEDF8 /* physfs.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = physfs.framework; path = /Library/Frameworks/physfs.framework; sourceTree = ""; }; A9F16926109E7BAD00FC83D1 /* libmodplug.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = libmodplug.framework; path = /Library/Frameworks/libmodplug.framework; sourceTree = ""; }; A9F169A6109E824900FC83D1 /* mpg123.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = mpg123.framework; path = /Library/Frameworks/mpg123.framework; sourceTree = ""; }; + CE73F7FF1EEB64150052DAB3 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.3.sdk/System/Library/Frameworks/AVFoundation.framework; sourceTree = DEVELOPER_DIR; }; FA0797981BF480A200034B7C /* GameController.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GameController.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.1.sdk/System/Library/Frameworks/GameController.framework; sourceTree = DEVELOPER_DIR; }; FA08F69116C765A200F007B5 /* love.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = love.framework; sourceTree = BUILT_PRODUCTS_DIR; }; FA0B7F061A95AAF3000E1D17 /* love.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = love.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -149,6 +151,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + CE73F8001EEB64150052DAB3 /* AVFoundation.framework in Frameworks */, FA5D24D11A96E73300C6FC8F /* liblove.a in Frameworks */, FA5D24C21A96D78000C6FC8F /* Foundation.framework in Frameworks */, FA5D24961A96CAC200C6FC8F /* CoreMotion.framework in Frameworks */, @@ -169,6 +172,7 @@ 1058C7A0FEA54F0111CA2CBB /* Frameworks */ = { isa = PBXGroup; children = ( + CE73F7FF1EEB64150052DAB3 /* AVFoundation.framework */, FA5D24801A96C97900C6FC8F /* ios */, FA0B7EEC1A959249000E1D17 /* macosx */, ); diff --git a/src/common/ios.mm b/src/common/ios.mm index 70a7f35ce..e34d8f362 100644 --- a/src/common/ios.mm +++ b/src/common/ios.mm @@ -349,25 +349,23 @@ void setAudioMixWithOthers(bool mixMode, bool playMuted) { @autoreleasepool { - NSError* err; + NSString *category = AVAudioSessionCategoryPlayback; + AVAudioSessionCategoryOptions options = 0; + NSError *err = nil; + if (mixMode) { if (playMuted) - { - [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&err]; - } + options = AVAudioSessionCategoryOptionMixWithOthers; else - { - [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&err]; - } + category = AVAudioSessionCategoryAmbient; } - else + else if (!playMuted) { - if (playMuted) - { - [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&err]; - } + category = AVAudioSessionCategorySoloAmbient; } + + [[AVAudioSession sharedInstance] setCategory:category withOptions:options error:&err]; if (err != nil) NSLog(@"Error in AVAudioSession setCategory: %@", [err localizedDescription]); } diff --git a/src/modules/audio/wrap_Audio.cpp b/src/modules/audio/wrap_Audio.cpp index 956f8da23..e00b0eb4a 100644 --- a/src/modules/audio/wrap_Audio.cpp +++ b/src/modules/audio/wrap_Audio.cpp @@ -25,7 +25,9 @@ #include "null/Audio.h" #include "common/runtime.h" +#ifdef LOVE_IOS #include "common/ios.h" +#endif // C++ #include @@ -294,6 +296,17 @@ int w_getDistanceModel(lua_State *L) return 1; } +#ifdef LOVE_IOS +int w_setMixMode(lua_State *L) +{ + love::ios::setAudioMixWithOthers(lua_toboolean(L, 1), lua_toboolean(L, 2)); +#else +int w_setMixMode(lua_State *) +{ +#endif + return 0; +} + int w_getShouldBeSilenced(lua_State *L) { #ifdef LOVE_IOS @@ -329,6 +342,7 @@ static const luaL_Reg functions[] = { "stopRecording", w_stopRecording },*/ { "setDistanceModel", w_setDistanceModel }, { "getDistanceModel", w_getDistanceModel }, + { "setMixMode", w_setMixMode }, { "getShouldBeSilenced", w_getShouldBeSilenced }, { 0, 0 } }; diff --git a/src/modules/love/love.cpp b/src/modules/love/love.cpp index f56bd7eee..64d3d4586 100644 --- a/src/modules/love/love.cpp +++ b/src/modules/love/love.cpp @@ -23,7 +23,6 @@ #include "common/version.h" #include "common/runtime.h" #include "common/wrap_Data.h" -#include "common/ios.h" #include "love.h" @@ -259,14 +258,6 @@ static int w__setGammaCorrect(lua_State *L) return 0; } -#ifdef LOVE_IOS -static int w__setAudioMixMode(lua_State *L) -{ - love::ios::setAudioMixWithOthers(lua_toboolean(L, 1), lua_toboolean(L, 2)); - return 0; -} -#endif - int luaopen_love(lua_State *L) { love::luax_insistpinnedthread(L); @@ -300,11 +291,6 @@ int luaopen_love(lua_State *L) lua_pushcfunction(L, w__setGammaCorrect); lua_setfield(L, -2, "_setGammaCorrect"); -#ifdef LOVE_IOS - lua_pushcfunction(L, w__setAudioMixMode); - lua_setfield(L, -2, "_setAudioMixMode"); -#endif - lua_newtable(L); for (int i = 0; love::VERSION_COMPATIBILITY[i] != nullptr; i++) diff --git a/src/scripts/boot.lua b/src/scripts/boot.lua index 418c557d3..3c6676879 100644 --- a/src/scripts/boot.lua +++ b/src/scripts/boot.lua @@ -420,10 +420,6 @@ function love.init() love._setGammaCorrect(c.gammacorrect) end - if love._setAudioMixMode then - love._setAudioMixMode(c.audio.mixmode, c.audio.playmuted) - end - -- Gets desired modules. for k,v in ipairs{ "thread", @@ -498,6 +494,10 @@ function love.init() end end + if love.audio then + love.audio.setMixMode(c.audio.mixmode, c.audio.playmuted) + end + -- Our first timestep, because window creation can take some time if love.timer then love.timer.step() diff --git a/src/scripts/boot.lua.h b/src/scripts/boot.lua.h index e53ba39ab..f6d4c8f67 100644 --- a/src/scripts/boot.lua.h +++ b/src/scripts/boot.lua.h @@ -781,13 +781,6 @@ const unsigned char boot_lua[] = 0x72, 0x72, 0x65, 0x63, 0x74, 0x28, 0x63, 0x2e, 0x67, 0x61, 0x6d, 0x6d, 0x61, 0x63, 0x6f, 0x72, 0x72, 0x65, 0x63, 0x74, 0x29, 0x0a, 0x09, 0x65, 0x6e, 0x64, 0x0a, - 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x5f, 0x73, 0x65, 0x74, 0x41, 0x75, 0x64, 0x69, 0x6f, - 0x4d, 0x69, 0x78, 0x4d, 0x6f, 0x64, 0x65, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, - 0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x5f, 0x73, 0x65, 0x74, 0x41, 0x75, 0x64, 0x69, 0x6f, 0x4d, 0x69, - 0x78, 0x4d, 0x6f, 0x64, 0x65, 0x28, 0x63, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2e, 0x6d, 0x69, 0x78, 0x6d, - 0x6f, 0x64, 0x65, 0x2c, 0x20, 0x63, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2e, 0x70, 0x6c, 0x61, 0x79, 0x6d, - 0x75, 0x74, 0x65, 0x64, 0x29, 0x0a, - 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x2d, 0x2d, 0x20, 0x47, 0x65, 0x74, 0x73, 0x20, 0x64, 0x65, 0x73, 0x69, 0x72, 0x65, 0x64, 0x20, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x73, 0x2e, 0x0a, 0x09, 0x66, 0x6f, 0x72, 0x20, 0x6b, 0x2c, 0x76, 0x20, 0x69, 0x6e, 0x20, 0x69, 0x70, 0x61, 0x69, 0x72, 0x73, @@ -918,6 +911,13 @@ const unsigned char boot_lua[] = 0x77, 0x2e, 0x69, 0x63, 0x6f, 0x6e, 0x29, 0x29, 0x0a, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x65, 0x6e, 0x64, 0x0a, + 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x20, 0x74, 0x68, 0x65, + 0x6e, 0x0a, + 0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2e, 0x73, 0x65, 0x74, 0x4d, 0x69, + 0x78, 0x4d, 0x6f, 0x64, 0x65, 0x28, 0x63, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2e, 0x6d, 0x69, 0x78, 0x6d, + 0x6f, 0x64, 0x65, 0x2c, 0x20, 0x63, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2e, 0x70, 0x6c, 0x61, 0x79, 0x6d, + 0x75, 0x74, 0x65, 0x64, 0x29, 0x0a, + 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x2d, 0x2d, 0x20, 0x4f, 0x75, 0x72, 0x20, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x65, 0x70, 0x2c, 0x20, 0x62, 0x65, 0x63, 0x61, 0x75, 0x73, 0x65, 0x20, 0x77, 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x20, 0x63, 0x72, 0x65, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x63, 0x61, 0x6e, 0x20, 0x74, 0x61, From 4e9212b152046b247a270619495a57486f5e02f2 Mon Sep 17 00:00:00 2001 From: Tiago Koji Castro Shibata Date: Thu, 13 Jul 2017 23:54:37 -0300 Subject: [PATCH 4/5] Add Android support to hasBackgroundMusic --- src/common/android.cpp | 16 ++++++++++++++++ src/common/android.h | 2 ++ src/modules/audio/wrap_Audio.cpp | 11 ----------- src/modules/system/System.cpp | 11 +++++++++++ src/modules/system/System.h | 8 ++++++++ src/modules/system/wrap_System.cpp | 14 ++++++++++++++ 6 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/common/android.cpp b/src/common/android.cpp index 872ec2d42..638162647 100644 --- a/src/common/android.cpp +++ b/src/common/android.cpp @@ -225,6 +225,22 @@ bool createStorageDirectories() return true; } +bool hasBackgroundMusic() +{ + JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv(); + jobject activity = (jobject) SDL_AndroidGetActivity(); + + jclass clazz(env->GetObjectClass(activity)); + jmethodID method_id = env->GetMethodID(clazz, "hasBackgroundMusic", "()Z"); + + jboolean result = env->CallBooleanMethod(activity, method_id); + + env->DeleteLocalRef(activity); + env->DeleteLocalRef(clazz); + + return result; +} + } // android } // love diff --git a/src/common/android.h b/src/common/android.h index 1e4e62a39..7a7f33dd0 100644 --- a/src/common/android.h +++ b/src/common/android.h @@ -66,6 +66,8 @@ bool mkdir(const char *path); bool createStorageDirectories(); +bool hasBackgroundMusic(); + } // android } // love diff --git a/src/modules/audio/wrap_Audio.cpp b/src/modules/audio/wrap_Audio.cpp index e00b0eb4a..2869b3adc 100644 --- a/src/modules/audio/wrap_Audio.cpp +++ b/src/modules/audio/wrap_Audio.cpp @@ -307,16 +307,6 @@ int w_setMixMode(lua_State *) return 0; } -int w_getShouldBeSilenced(lua_State *L) -{ -#ifdef LOVE_IOS - lua_pushboolean(L, love::ios::audioShouldBeSilenced()); -#else - lua_pushboolean(L, 0); -#endif - return 1; -} - // List of functions to wrap. static const luaL_Reg functions[] = { @@ -343,7 +333,6 @@ static const luaL_Reg functions[] = { "setDistanceModel", w_setDistanceModel }, { "getDistanceModel", w_getDistanceModel }, { "setMixMode", w_setMixMode }, - { "getShouldBeSilenced", w_getShouldBeSilenced }, { 0, 0 } }; diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index 5ad391188..5951449f6 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -184,6 +184,17 @@ void System::vibrate(double seconds) const #endif } +bool System::hasBackgroundMusic() const +{ +#if defined(LOVE_ANDROID) + return love::android::hasBackgroundMusic(); +#elif defined(LOVE_IOS) + return love::ios::audioShouldBeSilenced(); +#else + throw love::Exception("Unsupported platform."); +#endif +} + bool System::getConstant(const char *in, System::PowerState &out) { return powerStates.find(in, out); diff --git a/src/modules/system/System.h b/src/modules/system/System.h index 42bbc077f..5efa3abc9 100644 --- a/src/modules/system/System.h +++ b/src/modules/system/System.h @@ -106,6 +106,14 @@ public: */ virtual void vibrate(double seconds) const; + /** + * Gets if the user is playing music on background. + * Throws an exception on unsupported platforms. + * + * @return Whether a music is playing on background. + **/ + bool hasBackgroundMusic() const; + static bool getConstant(const char *in, PowerState &out); static bool getConstant(PowerState in, const char *&out); diff --git a/src/modules/system/wrap_System.cpp b/src/modules/system/wrap_System.cpp index a299583f8..fd2cee7aa 100644 --- a/src/modules/system/wrap_System.cpp +++ b/src/modules/system/wrap_System.cpp @@ -93,6 +93,19 @@ int w_vibrate(lua_State *L) return 0; } +int w_hasBackgroundMusic(lua_State *L) +{ + try + { + lua_pushboolean(L, instance()->hasBackgroundMusic()); + } + catch (love::Exception &) + { + lua_pushnil(L); + } + return 1; +} + static const luaL_Reg functions[] = { { "getOS", w_getOS }, @@ -102,6 +115,7 @@ static const luaL_Reg functions[] = { "getPowerInfo", w_getPowerInfo }, { "openURL", w_openURL }, { "vibrate", w_vibrate }, + { "hasBackgroundMusic", w_hasBackgroundMusic }, { 0, 0 } }; From 3dfe3808c645ac31b547d118e15b20922f31ee36 Mon Sep 17 00:00:00 2001 From: Tiago Koji Castro Shibata Date: Tue, 18 Jul 2017 15:15:04 -0300 Subject: [PATCH 5/5] Remove playmuted audio configuration --- src/common/ios.h | 4 ++-- src/common/ios.mm | 29 ++++++++++------------------- src/modules/audio/wrap_Audio.cpp | 2 +- src/modules/system/System.cpp | 4 ++-- src/modules/system/wrap_System.cpp | 9 +-------- src/scripts/boot.lua | 5 ++--- src/scripts/boot.lua.h | 12 ++++++------ 7 files changed, 24 insertions(+), 41 deletions(-) diff --git a/src/common/ios.h b/src/common/ios.h index 1b74a6a1e..1d93aa4b1 100644 --- a/src/common/ios.h +++ b/src/common/ios.h @@ -67,12 +67,12 @@ void vibrate(); /** * Enable mix mode (e.g. with background music apps) and playback with a muted device. **/ -void setAudioMixWithOthers(bool mixMode, bool playMuted); +void setAudioMixWithOthers(bool mixEnabled); /** * Returns whether another application is playing audio. **/ -bool audioShouldBeSilenced(); +bool hasBackgroundMusic(); } // ios } // love diff --git a/src/common/ios.mm b/src/common/ios.mm index e34d8f362..d7685873b 100644 --- a/src/common/ios.mm +++ b/src/common/ios.mm @@ -345,35 +345,26 @@ void vibrate() } } -void setAudioMixWithOthers(bool mixMode, bool playMuted) +void setAudioMixWithOthers(bool mixEnabled) { @autoreleasepool { - NSString *category = AVAudioSessionCategoryPlayback; - AVAudioSessionCategoryOptions options = 0; - NSError *err = nil; + NSString *category = AVAudioSessionCategorySoloAmbient; + NSError *err; - if (mixMode) - { - if (playMuted) - options = AVAudioSessionCategoryOptionMixWithOthers; - else - category = AVAudioSessionCategoryAmbient; - } - else if (!playMuted) - { - category = AVAudioSessionCategorySoloAmbient; - } + if (mixEnabled) + category = AVAudioSessionCategoryAmbient; - [[AVAudioSession sharedInstance] setCategory:category withOptions:options error:&err]; - if (err != nil) + if (![[AVAudioSession sharedInstance] setCategory:category error:&err]) NSLog(@"Error in AVAudioSession setCategory: %@", [err localizedDescription]); } } -bool audioShouldBeSilenced() +bool hasBackgroundMusic() { - return [[AVAudioSession sharedInstance] secondaryAudioShouldBeSilencedHint]; + if ([[AVAudioSession sharedInstance] respondsToSelector:@selector(secondaryAudioShouldBeSilencedHint)]) + return [[AVAudioSession sharedInstance] secondaryAudioShouldBeSilencedHint]; + return false; } } // ios diff --git a/src/modules/audio/wrap_Audio.cpp b/src/modules/audio/wrap_Audio.cpp index 2869b3adc..66e38f23d 100644 --- a/src/modules/audio/wrap_Audio.cpp +++ b/src/modules/audio/wrap_Audio.cpp @@ -299,7 +299,7 @@ int w_getDistanceModel(lua_State *L) #ifdef LOVE_IOS int w_setMixMode(lua_State *L) { - love::ios::setAudioMixWithOthers(lua_toboolean(L, 1), lua_toboolean(L, 2)); + love::ios::setAudioMixWithOthers(lua_toboolean(L, 1)); #else int w_setMixMode(lua_State *) { diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index 5951449f6..3d8ebd3fa 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -189,9 +189,9 @@ bool System::hasBackgroundMusic() const #if defined(LOVE_ANDROID) return love::android::hasBackgroundMusic(); #elif defined(LOVE_IOS) - return love::ios::audioShouldBeSilenced(); + return love::ios::hasBackgroundMusic(); #else - throw love::Exception("Unsupported platform."); + return false; #endif } diff --git a/src/modules/system/wrap_System.cpp b/src/modules/system/wrap_System.cpp index fd2cee7aa..925befba0 100644 --- a/src/modules/system/wrap_System.cpp +++ b/src/modules/system/wrap_System.cpp @@ -95,14 +95,7 @@ int w_vibrate(lua_State *L) int w_hasBackgroundMusic(lua_State *L) { - try - { - lua_pushboolean(L, instance()->hasBackgroundMusic()); - } - catch (love::Exception &) - { - lua_pushnil(L); - } + lua_pushboolean(L, instance()->hasBackgroundMusic()); return 1; } diff --git a/src/scripts/boot.lua b/src/scripts/boot.lua index 3c6676879..13ed3a891 100644 --- a/src/scripts/boot.lua +++ b/src/scripts/boot.lua @@ -374,8 +374,7 @@ function love.init() video = true, }, audio = { - mixmode = true, - playmuted = false, + mixwithsystem = true, -- Only relevant for Android / iOS. }, console = false, -- Only relevant for windows. identity = false, @@ -495,7 +494,7 @@ function love.init() end if love.audio then - love.audio.setMixMode(c.audio.mixmode, c.audio.playmuted) + love.audio.setMixMode(c.audio.mixwithsystem) end -- Our first timestep, because window creation can take some time diff --git a/src/scripts/boot.lua.h b/src/scripts/boot.lua.h index f6d4c8f67..4684661ab 100644 --- a/src/scripts/boot.lua.h +++ b/src/scripts/boot.lua.h @@ -683,9 +683,10 @@ const unsigned char boot_lua[] = 0x09, 0x09, 0x09, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a, 0x09, 0x09, 0x7d, 0x2c, 0x0a, 0x09, 0x09, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x20, 0x3d, 0x20, 0x7b, 0x0a, - 0x09, 0x09, 0x09, 0x6d, 0x69, 0x78, 0x6d, 0x6f, 0x64, 0x65, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a, - 0x09, 0x09, 0x09, 0x70, 0x6c, 0x61, 0x79, 0x6d, 0x75, 0x74, 0x65, 0x64, 0x20, 0x3d, 0x20, 0x66, 0x61, 0x6c, - 0x73, 0x65, 0x2c, 0x0a, + 0x09, 0x09, 0x09, 0x6d, 0x69, 0x78, 0x77, 0x69, 0x74, 0x68, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20, 0x3d, + 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x20, 0x2d, 0x2d, 0x20, 0x4f, 0x6e, 0x6c, 0x79, 0x20, 0x72, 0x65, 0x6c, + 0x65, 0x76, 0x61, 0x6e, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x41, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x20, + 0x2f, 0x20, 0x69, 0x4f, 0x53, 0x2e, 0x0a, 0x09, 0x09, 0x7d, 0x2c, 0x0a, 0x09, 0x09, 0x63, 0x6f, 0x6e, 0x73, 0x6f, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x2c, 0x20, 0x2d, 0x2d, 0x20, 0x4f, 0x6e, 0x6c, 0x79, 0x20, 0x72, 0x65, 0x6c, 0x65, 0x76, 0x61, 0x6e, 0x74, 0x20, @@ -914,9 +915,8 @@ const unsigned char boot_lua[] = 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, 0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2e, 0x73, 0x65, 0x74, 0x4d, 0x69, - 0x78, 0x4d, 0x6f, 0x64, 0x65, 0x28, 0x63, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2e, 0x6d, 0x69, 0x78, 0x6d, - 0x6f, 0x64, 0x65, 0x2c, 0x20, 0x63, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2e, 0x70, 0x6c, 0x61, 0x79, 0x6d, - 0x75, 0x74, 0x65, 0x64, 0x29, 0x0a, + 0x78, 0x4d, 0x6f, 0x64, 0x65, 0x28, 0x63, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2e, 0x6d, 0x69, 0x78, 0x77, + 0x69, 0x74, 0x68, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x29, 0x0a, 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x2d, 0x2d, 0x20, 0x4f, 0x75, 0x72, 0x20, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x65, 0x70, 0x2c, 0x20, 0x62, 0x65, 0x63, 0x61, 0x75, 0x73, 0x65, 0x20, 0x77, 0x69, 0x6e, 0x64,