Code fürs einmalige abspielen eines Sounds

Übersicht BlitzBasic Beginners-Corner

Neue Antwort erstellen

 

Kruemelator

Betreff: Code fürs einmalige abspielen eines Sounds

BeitragMo, Feb 23, 2009 17:38
Antworten mit Zitat
Benutzer-Profile anzeigen
Wie müsste der Code lauten wenn ich eine .wav Datei habe und diese, jedesmal wenn ich eine bestimmte Taste drück, nur einmal abspielen möchte. Dabei ist aber noch wichtig das wenn ich die Taste drücke die Sounddatei wieder von vorne gespielt wird, selbst wenn der Sound noch nicht zu ende gelaufen war.

Gruß Kruemelator

Xeres

Moderator

BeitragMo, Feb 23, 2009 17:43
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich empfehle die Online Hilfe unter PlaySound zu konsultieren.
Win10 Prof.(x64)/Ubuntu 16.04|CPU 4x3Ghz (Intel i5-4590S)|RAM 8 GB|GeForce GTX 960
Wie man Fragen richtig stellt || "Es geht nicht" || Video-Tutorial: Sinus & Cosinus
T
HERE IS NO FAIR. THERE IS NO JUSTICE. THERE IS JUST ME. (Death, Discworld)
 

DjDETE

BeitragMo, Feb 23, 2009 17:48
Antworten mit Zitat
Benutzer-Profile anzeigen
Und davor stopchannel, damit der Sound nicht 2x abgespielt wird:
Code: [AUSKLAPPEN]
sound=loadsound("sound.wav")
graphics 800,600
setbuffer backbuffer()
repeat
 cls
 text 20,20,"Drücke '1' um den Sound abzuspielen, ESC um zu beenden"
 if keyhit(2) then
  stopchannel channel
  channel=playsound(sound)
 endif
 if keyhit(1) then end
 flip
forever

MfG DeTe
Aktuelles Projekt: XXX |XXX | 3% der v1b fertig
www.emu-soft.de.vu Für die Homepage suche ich noch Linktauschpartner, bei Interesse einfach melden.
 

Kruemelator

BeitragMo, Feb 23, 2009 18:14
Antworten mit Zitat
Benutzer-Profile anzeigen
Danke für das schnelle antworten

Wenn ich den Code jetzt so übernehme und einmal die Taste 1 drücke wirdder Sound abgespielt. Das Problem dabei ist, dass er immer und immer wieder abgespielt wird. Ich wollte aber, dass er, wenn ich die Taste drücke, nur einmal abgespielt wird.

hectic

Sieger des IS Talentwettbewerb 2006

BeitragMo, Feb 23, 2009 18:17
Antworten mit Zitat
Benutzer-Profile anzeigen
Den Sound mit LoopSound versehen und den Rest über die Lautstärke ChannelVolume regeln.

Oder eine Abfrage machen, die verhindert, dass noch vor X MilliSecs ein neuer Sound abgespielt werden kann.
Download der Draw3D2 V.1.1 für schnelle Echtzeiteffekte über Blitz3D
 

DjDETE

BeitragMo, Feb 23, 2009 19:46
Antworten mit Zitat
Benutzer-Profile anzeigen
also bei mir wird der sound nur einmal abgespielt.
Was für eine BB-Version hast du denn?
MfG DeTe
Aktuelles Projekt: XXX |XXX | 3% der v1b fertig
www.emu-soft.de.vu Für die Homepage suche ich noch Linktauschpartner, bei Interesse einfach melden.

Holzchopf

Meisterpacker

BeitragMo, Feb 23, 2009 20:01
Antworten mit Zitat
Benutzer-Profile anzeigen
Es ist auch möglich in einer wav-Datei selber Loop-Points anzulegen. So weit ich weiss loopt BB ein Sample mit Loop-Points immer, egal ob LoopSound gesetzt ist oder nicht.

Such mal nach nem Soundprogramm, mit dem du diese entfernen könntest. Ich weiss grad nicht, ob Audacity das kann - Modplug Tracker kann das bestimmt, das weiss ich, aber das ist eigentlich eben ein Tracker und der Sample-Editor nur Beigemüse, aber zur Not tuts das auch.
Erledige alles Schritt um Schritt - erledige alles. - Holzchopf
CC BYBinaryBorn - Yogurt ♫ (31.10.2018)
Im Kopf da knackt's und knistert's sturm - 's ist kein Gedanke, nur ein Wurm

Abrexxes

BeitragMo, Feb 23, 2009 20:56
Antworten mit Zitat
Benutzer-Profile anzeigen
Der Code hier entfernt eventuelle Loop Points, ganz einfach mal so Wink

Code: [AUSKLAPPEN]

;Abrexxes 2007
;With this you can read, write a wav header and make sample loops for blitzbasic.
;You dont need Loopsound () but make perfect loops. ;)
;You dont need this global, but you can use it to see the data size of your wav file for max value

Global s_datalenght%

Graphics 640,480,0,2
SetBuffer BackBuffer ()

;LoadLoopSound("test.wav",0,0) ; no loop
LoadLoopSound("test.wav",1,s_datalenght) ; maximum loop
;LoadLoopSound("test.wav",800,8000) ; what you want but not more then datalenght

musik = LoadSound ("s_temp.wav")
DeleteFile "s_temp.wav"


While Not KeyHit (1)
Cls
If KeyHit (57) Then
PlaySound (musik)
EndIf
Text 10,10,"Press space to play"
Text 10,20, "Datasize = "+s_datalenght
Flip
Wend
End



Function LoadLoopSound(s_file$,s_start%,s_end%)

;**********************************
;**** Read Source Wav File *****
;**********************************
s_source = ReadFile (s_file)

; read RIFF header (1179011410) to see if it is a wav file
s_check = ReadInt (s_source)
      If s_check = 1179011410 Then ; RIFF found
            s_check = ReadInt (s_source)
            s_sourcesize = (s_check + 8) ; size of sourcefile
            s_check = ReadInt (s_source) ; read "wave"
      EndIf   
If Not s_check = 1163280727 Then Return 0 ; no wave..go away with that :)
                     
; search and read   FMT chunk for sample infos     
s_position = 12
s_goal = 0
Repeat
      SeekFile s_source, s_position
            s_check = ReadInt (s_source)
                  If s_check = 544501094 Then ; FMT found
                        s_goal = 1 ; now lets read and store all the infos
                              s_fmtsize = ReadInt (s_source) ;<fmt length>  size of this chunk, normal = 16 byte from here
                              s_format = ReadShort (s_source)   ;<format tag>    sample format, normal = 1 (hex) for PCM
                              s_channels = ReadShort (s_source) ;<channels>  1 = mono, 2 = stereo
                              s_rate = ReadInt (s_source) ;<sample rate>    (hz)
                              s_bytes = ReadInt (s_source) ;<bytes/second>    Sample-Rate * Block-Align
                              s_block = ReadShort (s_source) ;<block align>    Channels * bits/sample / 8
                              s_bit = ReadShort (s_source) ;<bits/sample>    8, 16 or 24
                  EndIf
            s_position = s_position +1 ;FMT not found, next step
If s_goal = 1 Then Exit   ; ok   
Until Eof (s_source) = 1
If s_goal = 0 Then Return 0 ; no FMT found, Error

; search and read DATA chunk
s_position =12
s_goal = 0
Repeat
      SeekFile s_source, s_position
            s_check = ReadInt (s_source)
            If s_check = 1635017060 Then ; DATA found
                  s_goal = 1
                  s_datalenght = ReadInt (s_source) ;<length> of data block
                  sbank_cache = CreateBank (s_datalenght) ; now we store the sample data
                        For i = 0 To (s_datalenght-1)
                              s_value = ReadByte (s_source)
                              PokeByte sbank_cache,i,s_value
                        Next
            EndIf
      s_position = s_position +1 ; DATA not found, next step
If s_goal = 1 Then Exit ; ok
Until Eof (s_source) = 1
If s_goal = 0 Then Return 0 ; no DATA found, Error

CloseFile s_source ; we have what we need, all other chunks we can ignore

;**********************************
;**** Write temp Wav File *****
;**********************************

s_temp = WriteFile ("s_temp.wav") ; create a temp

;write RIFF header
WriteInt s_temp, 1179011410 ; RIFF
WriteInt s_temp, s_sourcesize-8 ;size of header
WriteInt s_temp, 1163280727 ; WAVE

;write FMT chunk
WriteInt s_temp, 544501094 ; FMT
WriteInt s_temp, s_fmtsize ;<fmt length>
WriteShort s_temp, s_format ;<format tag>
WriteShort s_temp, s_channels ;<channels>
WriteInt s_temp, s_rate ;<sample rate>
WriteInt s_temp, s_bytes ;<bytes/second>
WriteShort s_temp, s_block ;<block align>
WriteShort s_temp, s_bit ;<bits/sample>
                 
;write SMPL chunk
WriteInt s_temp, 1819307379 ;SMPL
WriteInt s_temp, 60 ;36 + 1 sample loop section (24)
WriteInt s_temp, 0 ; Manufacturer , we dont need   
WriteInt s_temp, 0   ; ProductID , we dont need
WriteInt s_temp, 0   ;Sample Period    ,not supported by blitzbasic
WriteInt s_temp, 0   ;MIDI Unity Note    ,not supported by blitzbasic
WriteInt s_temp, 0   ;MIDI Pitch Fraction    ,not supported by blitzbasic
WriteInt s_temp, 0   ;SMPTE Format    ,not supported by blitzbasic
WriteInt s_temp, 0   ;SMPTE Offset    ,not supported by blitzbasic
WriteInt s_temp, 0 ;Num Sample Loops    ,not supported by blitzbasic
WriteInt s_temp, 0   ;Sampler Data    ,not supported by blitzbasic

WriteInt s_temp, 0 ;Cue Point ,not supported by blitzbasic
WriteInt s_temp, 0 ;Type    ,not supported by blitzbasic
WriteInt s_temp, s_start ;   Start loop
WriteInt s_temp, s_end ;End loop
WriteInt s_temp, 0 ;Fraction ,not supported by blitzbasic
WriteInt s_temp, 0 ;Play Count ,not supported by blitzbasic

;write DATA chunk & data
WriteInt s_temp, 1635017060 ; DATA
WriteInt s_temp, s_datalenght ;<length>
WriteBytes (sbank_cache,s_temp,0,s_datalenght) ;sample data from bank
FreeBank sbank_cache ; we no need this longer

CloseFile s_temp

End Function
;~IDEal Editor Parameters:
;~C#Blitz3D


cu
 

Kruemelator

BeitragMo, Feb 23, 2009 22:21
Antworten mit Zitat
Benutzer-Profile anzeigen
@alle Danke für Hilfe
Lag wirklich an der .wav Datei. Wenn ich sie als .mp3 extrahiere wird sie nur einmal abgepielt, außerdem braucht sie dann weniger Platz.

hectic

Sieger des IS Talentwettbewerb 2006

BeitragMo, Feb 23, 2009 22:45
Antworten mit Zitat
Benutzer-Profile anzeigen
Aus lizenztechnischen Gründen würde ich statt des .mp3-Formates lieber auf das .ogg-Format setzen. Diese kann Blitz3D problemlos abspielen.
Download der Draw3D2 V.1.1 für schnelle Echtzeiteffekte über Blitz3D

Neue Antwort erstellen


Übersicht BlitzBasic Beginners-Corner

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group